I've created a route in a Node.js project to render all "logs" from a MongoDB database to a Web page:
app.get('/api/logs', function (req, res) {
Log.find( function (err, logs) {
res.json(logs);
});
});
I want to modify this query to (1) limit the response to 10 logs and (2) display the logs in reverse chronological order (from most recent to least recent). If I try the below code, nothing will render to my page and my Node server gives the following error: Error: sort() only takes 1 Argument.
app.get('/api/logs', function (req, res) {
Log.find().limit(10).sort({$natural:-1}, function(err, logs){
res.json(logs);
});
});
The above snippet DOES work if I type it directly into my Monog console as a single query: Log.find().limit(10).sort({$natural:-1}) Is there a different way to write this to grab the information I want? Thanks for your suggestions!
_idfield, contains a timestamp and sorts accordingly.Log.find().sort({ "_id": -1 }).limit(10),function(err,logs) { ... })