I want to extract only 1 document from mongo db 'property' collection. It should also be the latest document. It looks very simple but I dont know why my query is not retrieving number of document specified in limit.
ORIGINAL METHOD: This query extracts all documents
app.get('/', function(req, res){
db.property.find(function (err, docs) {
res.render("index.ejs",{property: docs});
})
});
EDIT 1: This query extracts nothing
app.get('/', function(req, res){
db.property.find( {sort: {created_at: -1}, limit: 1}, function (err, docs) {
res.render("index.ejs",{property: docs});
})
});
EDIT 2: This query gives correct 1 document but I want the latest one now.
app.get('/', function(req, res){
db.property.find({}).limit(2).toArray(function (err, docs) {
res.render("index.ejs",{property: docs});
})
});
Any help would be greatly appreciated.
Thank you
I tried modifying query in several ways and finally I got its solution
app.get('/', function(req, res){
db.property.find({}).sort({timestamp: -1}).limit(1).toArray(function (err, docs) {
res.render("index.ejs",{property: docs});
})
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With