且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

mongodb 中的最大值和最小值

更新时间:2023-01-20 16:09:02

您需要使用 .aggregate() 方法使其工作.

You need to use the .aggregate() method for it to work.

db.collection.aggregate([ 
    { "$group": { 
        "_id": null,
        "max": { "$max": "$price" }, 
        "min": { "$min": "$price" } 
    }}
])

_id 字段在 $group 阶段并找到 max/min 孔集合的价格值而不是特殊组它需要设置为 null 否则查询将只返回最大值/min 每组

The _id field is mandatory in the $group stage and to find the max/min values for price for the hole collection not for special group it needs to be set to null otherwise the query will only return max/min for each group