[英]MongoDB: Get single document with specific parent id and highest order value
我的文档具有键order
,这是一个递增的数值。
现在,我想获得所有具有特定父值的文档的最高订单价值。
我正在做的是(我正在使用mongo本机驱动程序)
我正在这样做,因为我认为findOne
无法对文档进行排序。 我说的对吗?
const last = await Content.find({ parent: parentID }).sort({ order: -1 }).limit(1).toArray()
console.log(last[0].order)
现在,我得到了一个包含单个文档的数组。 但是是否可以只获取文档本身而不将结果作为数组返回?
您想像这样使用findOne :
var options = {
"sort": [['order','desc']]
};
Content.findOne({ parent: parentID }, options, function(err, result) {
if (err) throw err;
console.log(result);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.