MongoDB 排序无效

MongoDB sort does not work

从今天早上开始(我想)我无法整理我的 collection。 collection 里面有 26944 个 object(这是我目前最大的 - 但这不是原因吧?)。

所以我在做什么:

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

效果很好,没有任何问题! 但是:

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

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

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

Returns 结果为 26944 Objects,

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

将 return 0 Objects.

我是不是做错了什么? 也许我的 collection 崩溃了?有人知道在这种情况下可以做什么吗? (删除 collection 除外)

示例Object:

{
    "_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
}

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

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