【发布时间】: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