My documents have the key order
, which is an ascending numeric value.
Now I would like to get the highest order value of all documents which has a specific parent value.
What I am doing is (I'm using mongo native driver)
I'm doing this, as I think findOne
can't sort the documents. Am I right with that?
const last = await Content.find({ parent: parentID }).sort({ order: -1 }).limit(1).toArray()
console.log(last[0].order)
Now I get an array with a single document. But is it possible to get only the document itself without returning the result as an array?
You want to use findOne like this:
var options = {
"sort": [['order','desc']]
};
Content.findOne({ parent: parentID }, options, function(err, result) {
if (err) throw err;
console.log(result);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.