Max and min in mongodb

后端 未结 1 1701
無奈伤痛
無奈伤痛 2020-12-06 14:11

I have the below DB like entries :

My insert query

 db.collection.insert({company:\"C3\",model:\"M3\",price:55600,discount:9,resolution:\"1         


        
相关标签:
1条回答
  • 2020-12-06 14:54

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

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

    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

    0 讨论(0)
提交回复
热议问题