【问题标题】:MongoDB sort does not workMongoDB 排序不起作用
【发布时间】:2015-05-19 16:44:51
【问题描述】:

从今天早上开始(我想)我无法整理我的收藏。该集合里面有 26944 个对象(这是目前我最大的 - 但这不可能是原因吧?)。

所以我在做什么:

myCollection.find().sort({ _id: 1 })

这很好用,没有任何问题! 但是:

myCollection.find().sort({ xxx: 1 })

每个其他情况都不起作用,xxx 表示我的对象上的任何其他键,我拿哪个都没关系... 例如:

myCollection.find({modified: { $exists : true } })

返回包含 26944 个对象的结果,

myCollection.find({modified: { $exists : true } }).sort({"modified":-1})

将返回 0 个对象。

我有什么问题吗? 也许我的收藏已经崩溃了?有人知道在这种情况下可以做什么吗? (除了删除集合)

示例对象:

{
    "_id": ObjectId("55071e25760e250d050ed8d5"),
    "sysModified": new Date("2015-03-15T21:10:12+0100"),
    "created": new Date(1426529829922),
    "modified": new Date(1426528563945),
    "payments": [

    ],
    "orderItems": [
        {
            "orderId": 'xxxxx',
            "itemId": 'xxxxx',
            "ean": "XXXXX"
            "quantity": 1,
            "name": "xxxxxxx",
            "vat": 19,
            "price": 29.989999999999998437,
            "currency": "EUR",
            "_id": ObjectId("5507365c89866d820dcef7e0"),
        },
        {
            "orderId": 'xxxxx',
            "itemId": 'xxxxx',
            "ean": "XXXXX"
            "quantity": 1,
            "name": "xxxxxxx",
            "vat": 19,
            "price": 29.989999999999998437,
            "currency": "EUR",
            "_id": ObjectId("5507365c89866d820dcef7e1"),
        }
    ],
    "orderData": {
        "orderId": XXXXXXXXX,
        "type": "order",
        "status": "XXXXXXXXX",
        "timestamp_php": XXXXXXXXX,
        "customer": XXXXXXXXX,
        ...
    },
    "orderDeliveryAddress": {
        "customer": xxxxxxxxxxx,
        "company": "",
        "additional": "",
        "firstName": "xxxxxxxxxxx",
        "surname": "xxxxxxxxxxx",
        "street": "xxxxxxxxxxx",
        "houseNumber": "xxxxxxxxxxx",
        "zip": "xxxxxxxxxxx",
        "city": "xxxxxxxxxxx",
        "iso2": "xxxxxxxxxxx",
        "phone": "xxxxxxxxxxx",
        "fax": "xxxxxxxxxx",
        "email": "xxxxxxxxxxx",
    },
    "orderCustomerAddress": {
        "customer": xxxxxxxxxxx,
        "company": "",
        "additional": "",
        "firstName": "xxxxxxxxxxx",
        "surname": "xxxxxxxxxxx",
        "street": "xxxxxxxxxxx",
        "houseNumber": "xxxxxxxxxxx",
        "zip": "xxxxxxxxxxx",
        "city": "xxxxxxxxxxx",
        "iso2": "xxxxxxxxxxx",
        "phone": "xxxxxxxxxxx",
        "fax": "xxxxxxxxxx",
        "email": "xxxxxxxxxxx",
    },
    "__v": 1
}

【问题讨论】:

  • 您能否举例说明数据的外观、排序方式以及您预期的排序顺序?顺便提一句。您排序的字段上有键吗?
  • 看到你不能对一个不存在的键进行排序,所以你需要比你现在做的更具体。任何“不存在”的东西都有一个预定义的值null,因此有一个预分配的排序顺序。
  • @Lunn,没什么特别的,我在 shell、nodeJS 和 MongoHub(它是 osx 上的 mongo 工具)中运行 find 命令,每个都给出相同的结果(没什么)。
  • 好吧,我得到了一些东西,...如果我添加一个查询(用于查找)它可以工作,但不是所有... {'orderData.orderId': { $gt : 30000 } } 所以我想我在 orderId 较低的对象上遇到了错误,但它可能是哪个错误?
  • 真的不清楚。我看到的问题是您没有以它可以的方式对定义的值进行排序。您需要在问题中明确表达您的意图。

标签: mongodb sorting mongodb-query


【解决方案1】:

您的收藏有可能很大。如果您尝试对一个没有索引的键进行排序,如果结果集太大,这可能会导致错误。错误应该如下所示:

error: {
        "$err" : "too much data for sort() with no index.  
                  add an index or specify a smaller limit",
        "code" : 10128
}

你可以做的是为你想要排序的键添加一个索引:

db.myCollection.ensureIndex({"modified":-1})

或者你在排序之前减少结果集:

myCollection.find({modified: { $exists : true } }).limit(10).sort({"modified":-1})

【讨论】:

  • 鉴于问题包含显示索引使用情况的.explain() 输出,为什么这会适用?不是很细心。
  • @NeilLunn:我没有看到任何解释声明。您确定您回答的问题正确吗?
  • 可能是我自己在考虑另一个问题(有很多)时的错误陈述,但我认为您的“解决方案”除了已经在问题中说明的尝试之外没有其他任何内容。也许他们弄错了。但很有可能,cmets 在回应中比猜测更能说明问题。
猜你喜欢
  • 1970-01-01
  • 2019-12-05
  • 2019-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多