【问题标题】:Why has mongo ascending/descending sorting order flipped for a Date field?为什么日期字段的 mongo 升序/降序排序顺序翻转?
【发布时间】:2014-06-22 05:44:10
【问题描述】:

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") }

【问题讨论】:

    标签: mongodb sorting


    【解决方案1】:

    你有该字段的索引吗?

    除非您有指定键模式的索引,否则请使用 $orderby 结合 $maxScan 和/或 cursor.limit() 以避免需要 MongoDB 执行大型内存排序。

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

    【讨论】:

    • 更新了 qn。但我认为有一个索引对此并不重要。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-08
    • 2014-09-08
    • 2013-02-17
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    相关资源
    最近更新 更多