Tuesday, 15 September 2015

javascript - In mongoDB, how can I find the previous doc by _id? -



javascript - In mongoDB, how can I find the previous doc by _id? -

i'm trying homecoming previous , next mongo doc _id. first 1 below works next, latter doesn't homecoming previous. returns first doc. tried using $sort didn't seem anything.

posts.findone({'_id': {'$gt': identity}}, function(err, post) { "use strict"; if (err) homecoming callback(err, null); callback(err, post); }); posts.findone({'_id': {'$lt': identity} }, {'$sort':-1}}, function(err, post) { "use strict"; if (err) homecoming callback(err, null); callback(err, post); });

that's not right syntax sort option. be:

posts.findone({_id: {$lt: identity}}, {sort: {_id: -1}}, function(err, post) {

javascript mongodb

No comments:

Post a Comment