MongoDB distinct aggregation

前端 未结 4 600
粉色の甜心
粉色の甜心 2020-12-08 06:43

I\'m working on a query to find cities with most zips for each state:

db.zips.distinct(\"state\", db.zips.aggregate([ {$group:{_id:{state:\"$state\", city:\"         


        
4条回答
  •  情书的邮戳
    2020-12-08 07:08

    You can use $addToSet with the aggregation framework to count distinct objects.

    For example:

    db.collectionName.aggregate([{
        $group: {_id: null, uniqueValues: {$addToSet: "$fieldName"}}
    }])
    

提交回复
热议问题