Reputation: 1258
I have a trouble with the code below. I try to get max(seq) in 'links' collection. After run the code below, I get "FATAL ERROR: JS Allocation failed - process out of memory" error. How can I overcome that problem? Thank you for your replies.
db.collection('links', function(err, collection) {
var options = { "limit": 1, "sort": [['seq','desc']]};
collection.find({}, options , function(err, docs) {
console.log("Returned #" + docs.seq + " documents");
});
});
Note: Memory usage of node has incredibly increased after running, by the way.
Upvotes: 2
Views: 1442
Reputation: 312025
Add an index on seq
to your collection so that Mongo doesn't have to load the whole collection into memory to sort it.
Also, use findOne
instead of find
as the docs
parameter in your current code is actually a cursor object, not the doc with the max seq
. As in:
var options = { "sort": [['seq','desc']] };
collection.findOne({}, options , function(err, doc) {
console.log("Returned #" + doc.seq + " documents");
});
Upvotes: 1