在 mongodb 集合中查找多次出现的值
Finding values in mongodb collection with multiple occurence
我有一个文件集合如下:
{id: 1, "year": "12", "type": "checking", "location": "nyc", "category" : "Admin"}
{id: 2, "year": "15", "type": "checking", "location": "ma", "category": "Normal"}
{id: 3, "year": "12", "type": "credit", "location": "nyc","category": "Admin"}
{id: 4, "year": 12, "type": "checking", "location": "nyc", "category" : "Admin"}
现在,我想计算出现不止一次满足条件 location:nyc、year:12 的 'type',类别:管理员
所以想要的结果是
{"Checking": 2}
有什么方法可以达到上面的效果?
使用 $match 和 $group
db.collectionname.aggregate({$match: {location: 'nyc', year:'12', category:'Admin'}},
{$group: {_id:'$type', count:{$sum:1}}},
{$match:{count: {$gt:1}}}
)
我有一个文件集合如下:
{id: 1, "year": "12", "type": "checking", "location": "nyc", "category" : "Admin"}
{id: 2, "year": "15", "type": "checking", "location": "ma", "category": "Normal"}
{id: 3, "year": "12", "type": "credit", "location": "nyc","category": "Admin"}
{id: 4, "year": 12, "type": "checking", "location": "nyc", "category" : "Admin"}
现在,我想计算出现不止一次满足条件 location:nyc、year:12 的 'type',类别:管理员
所以想要的结果是
{"Checking": 2}
有什么方法可以达到上面的效果?
使用 $match 和 $group
db.collectionname.aggregate({$match: {location: 'nyc', year:'12', category:'Admin'}},
{$group: {_id:'$type', count:{$sum:1}}},
{$match:{count: {$gt:1}}}
)