mongodb - 为什么 mongo 的升序/降序排序顺序会为日期字段翻转?

Mongo 版本:2.6.0

我不确定我是否理解这一点。似乎 mongo DB 排序顺序以某种方式翻转了我的一个字段?

这里是升序排序:

> db.clicks.find({}, {"time": 1}).sort({"time": 1}).limit(1).pretty()
{
        "_id" : ObjectId("5367e87fb820307109000cdc"),
        "time" : ISODate("2014-05-05T19:37:25Z")
}

和降序排序:

> db.clicks.find({}, {"time": 1}).sort({"time": -1}).limit(1).pretty()
{
        "_id" : ObjectId("53607a5bb820301547000242"),
        "time" : ISODate("2014-04-30T04:21:31Z")
}

这种翻转只发生在 time 字段。

如果我在另一个字段上排序,它工作正常:

> db.clicks.find({}, {"lid": 1}).sort({"lid": 1}).limit(1).pretty()
{ "_id" : ObjectId("5363f859b820306ca20002aa"), "lid" : 55960 }
> db.clicks.find({}, {"lid": 1}).sort({"lid": -1}).limit(1).pretty()
{ "_id" : ObjectId("5364488bb8203071090002db"), "lid" : 2074671 }

有什么想法吗?

更新:看起来有些奇怪的事情正在发生,因为获取更多结果给出了一个完全随机的顺序。

> db.clicks.find({}, {"time": 1}).sort({"time": 1}).limit(5).pretty()
{
        "_id" : ObjectId("5367e87fb820307109000cdc"),
        "time" : ISODate("2014-05-05T19:37:25Z")
}
{
        "_id" : ObjectId("53618d7eb820306ca200002e"),
        "time" : ISODate("2014-04-30T23:55:30Z")
}
{
        "_id" : ObjectId("53675870b820307109000876"),
        "time" : ISODate("2014-05-05T09:22:48Z")
}
{
        "_id" : ObjectId("53642974b820307109000072"),
        "time" : ISODate("2014-05-02T23:25:20Z")
}
{
        "_id" : ObjectId("535e6a11b8203015470000e5"),
        "time" : ISODate("2014-04-28T14:47:25Z")
}

time 是一个索引字段(用于 TTL):

> db.clicks.getIndexes()
[
    ...
        {
                "v" : 1,
                "key" : {
                        "time" : 1
                },
                "name" : "time_1",
                "ns" : "raw.clicks",
                "expireAfterSeconds" : 7776000
        }
]

更新 2:我删除了索引并重建了它。

> db.clicks.dropIndex({"time": 1})
{ "nIndexesWas" : 10, "ok" : 1 }
> db.clicks.ensureIndex({"time": 1}, {"expireAfterSeconds" : 7776000})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 9,
        "numIndexesAfter" : 10,
        "ok" : 1
}

随机排序仍在发生。

然后我从集合中删除了所有文档:

> db.clicks.remove({})
WriteResult({ "nRemoved" : 130 })

并添加新文档并重新运行查询:

> db.clicks.find({}, {"time": 1}).sort({"time": 1}).limit(5).pretty()
{
        "_id" : ObjectId("5367f9a2b820307109000f2e"),
        "time" : ISODate("2014-05-05T20:50:36Z")
}
{
        "_id" : ObjectId("5367f9ccb820307109000f6f"),
        "time" : ISODate("2014-05-05T20:51:04Z")
}
{
        "_id" : ObjectId("5367f9b7b820307109000f3d"),
        "time" : ISODate("2014-05-05T20:50:54Z")
}
{
        "_id" : ObjectId("5367f9b7b820307109000f3e"),
        "time" : ISODate("2014-05-05T20:50:58Z")
}
{
        "_id" : ObjectId("5367f9b7b820307109000f3f"),
        "time" : ISODate("2014-05-05T20:51:00Z")
}

还是一样的问题。是因为时间字段的索引是TTL索引吗?

更新 3:好的,所以这个特定的 mongo 服务器出了点问题,我不太清楚。我在另一个 mongo 服务器上运行查询,查询工作正常(集合和索引相同):

> db.clicks.find({}, {"_id": 0, "time": 1}).sort({"time": 1}).limit(10).pretty()
{ "time" : ISODate("2014-04-25T23:04:57Z") }
{ "time" : ISODate("2014-04-25T23:04:57Z") }
{ "time" : ISODate("2014-04-25T23:04:58Z") }
{ "time" : ISODate("2014-04-25T23:04:59Z") }
{ "time" : ISODate("2014-04-25T23:04:59Z") }
{ "time" : ISODate("2014-04-25T23:04:59Z") }
{ "time" : ISODate("2014-04-25T23:05:03Z") }
{ "time" : ISODate("2014-04-25T23:05:09Z") }
{ "time" : ISODate("2014-04-25T23:05:10Z") }
{ "time" : ISODate("2014-04-25T23:05:10Z") }

> db.clicks.find({}, {"_id": 0, "time": 1}).sort({"time": -1}).limit(10).pretty()
{ "time" : ISODate("2014-05-05T22:30:22Z") }
{ "time" : ISODate("2014-05-05T22:30:19Z") }
{ "time" : ISODate("2014-05-05T22:30:19Z") }
{ "time" : ISODate("2014-05-05T22:30:18Z") }
{ "time" : ISODate("2014-05-05T22:30:16Z") }
{ "time" : ISODate("2014-05-05T22:30:16Z") }
{ "time" : ISODate("2014-05-05T22:30:16Z") }
{ "time" : ISODate("2014-05-05T22:30:15Z") }
{ "time" : ISODate("2014-05-05T22:30:14Z") }
{ "time" : ISODate("2014-05-05T22:30:13Z") }

最佳答案

你有这个领域的索引吗?

Unless you have an index for the specified key pattern, use $orderby in conjunction with $maxScan and/or cursor.limit() to avoid requiring MongoDB to perform a large in-memory sort.

发件人:http://docs.mongodb.org/manual/reference/operator/meta/orderby/

https://stackoverflow.com/questions/23480675/

相关文章:

r - 警告 : replacing previous import ‘head’ when loa

xml - 使用 XSLT 转换在 WordProcessingML 中保留换行符和空格

session - Laravel 4.1 session 变量被随机遗忘

php - Laravel:模型内验证。多重验证规则

python - 用python编写的鼠标运动跟踪程序

asp.net-web-api - 当 WebApi 序列化失败时,我希望异常冒泡而不是 null

macos - 通过 XPC 与应用程序通信并启动以 root 身份运行的守护进程

ios - 解码Code128条码?

sql - 在 NLTK 中将英语转换为 SQL

.net - 每个文件只执行一个类