简体   繁体   English

Mongodb排序返回null而不是数据

[英]Mongodb Sorting returns null instead of data

My Mongodb dataset is like this 我的Mongodb数据集是这样的

{
    "_id" : ObjectId("5a27cc4783800a0b284c7f62"),
    "action" : "1",
    "silent" : "0",
    "createdate" : ISODate("2017-12-06T10:53:59.664Z"),
    "__v" : 0
}

Now I have to find the data whose Action value is 1 and silent value is 0. one more thing is that all the data returns is descending Order. 现在,我必须找到其Action值为1而silent值为0的数据。另一件事是,所有返回的数据都是降序Order。 My Mongodb Query is 我的Mongodb查询是

db.collection.find({'action': 1, 'silent': 0}).sort({createdate: -1}).exec(function(err, post) {
            console.log(post.length);
});

Earlier It works Fine for me But Now I have 121000 entry on this Collection. 之前它对我来说很好,但是现在我有121000个该收藏夹条目。 Now it returns null. 现在,它返回null。

I know there is some confusion on .sort()

If i remove the sort Query then everything is fine. 如果我删除排序查询,那么一切都很好。 Example

db.collection.find({'action': 1, 'silent': 0}).exec(function(err, post) {
            console.log(post.length);// Now it returns data but not on Descending order
});

MongoDB limits the amount of data it will attempt to sort without an index . MongoDB 限制了它将尝试不使用索引进行排序的数据量。 This is because Mongo has to sort the data in memory or on disk, both of which can be expensive operations, particularly for queries run frequently. 这是因为Mongo必须对内存或磁盘中的数据进行排序,这两种操作都是昂贵的操作,尤其是对于频繁运行的查询。

  • In most cases, this can be alleviated by creating indexes on the fields you sort on. 在大多数情况下,可以通过在排序的字段上创建索引来缓解这种情况。

you can create index with :- 您可以使用:-创建索引

db.myColl.createIndex( { createdate: 1 })

thanks ! 谢谢 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM