我使用聚合框架从一组文档的集合中获取字段的最大值,如下所示
db.getCollection('Collection').aggregate([ {$match: {"product": {$in: ["product-a"]}}}, {$group: {_id: null, maxPrice: {$max: "$price"}}} ])
但是如果我想得到符合这个条件的文档的ID呢?在管道的下一个阶段,我该怎么做?
你可以把所有的田地 $max 像这样的接线员。。。它将为您提供max字段的文档,该字段最初用于 $最大值 反对。只是我用过 price 在这里。
$max
$最大值
price
db.getCollection("Collection").aggregate([ { "$match": { "product": { "$in": ["product-a"] } } }, { "$group": { "_id": null, "maxPrice": { "$max": { "price": "$price", "_id": "$_id" } } }} ])