Iron_Man
Iron_Man

Reputation: 161

Mongodb Sorting returns null instead of data

My Mongodb dataset is like this

{
    "_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. My Mongodb Query is

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. Now it returns 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
});

Upvotes: 0

Views: 68

Answers (1)

gajju_15
gajju_15

Reputation: 527

MongoDB limits the amount of data it will attempt to sort without an index . 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.

  • 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 !

Upvotes: 1

Related Questions