我的Mongodb数据集就像这样
{
"_id" : ObjectId("5a27cc4783800a0b284c7f62"),
"action" : "1",
"silent" : "0",
"createdate" : ISODate("2017-12-06T10:53:59.664Z"),
"__v" : 0
}
现在我必须找到Action值为1且静默值为0的数据。还有一件事是所有数据返回都是降序。 我的Mongodb查询是
db.collection.find({'action': 1, 'silent': 0}).sort({createdate: -1}).exec(function(err, post) {
console.log(post.length);
});
早些时候它对我来说很好但是现在我在这个Collection上有121000个条目。现在它返回null。
I know there is some confusion on .sort()
如果我删除排序查询,那么一切都很好。实施例
db.collection.find({'action': 1, 'silent': 0}).exec(function(err, post) {
console.log(post.length);// Now it returns data but not on Descending order
});
答案 0 :(得分:1)
MongoDB 限制在没有索引的情况下尝试排序的数据量。 这是因为Mongo必须对内存或磁盘上的数据进行排序,这两种操作都是昂贵的操作,特别是对于经常运行的查询。
您可以使用以下方式创建索引: -
db.myColl.createIndex( { createdate: 1 })
谢谢!