这是我的收藏结构......
{
"_id": ObjectId("5316f53228551c0af600019b"),
"age_range": {
"min": NumberInt(21)
},
"likes": {
"data": {
"0": {
"category": "Tv show",
"name": "Those Who Kill on A&E",
"created_time": "2014-03-03T15:08:02+0000",
"id": "629689387051864"
},
"1": {
"category": "Journalist",
"name": "Robin Roberts",
"created_time": "2014-02-24T18:02:43+0000",
"id": "780950061932847"
}
},
{
"_id": ObjectId("5316f53228551c0af600019b"),
`
我需要获取每个类别的用户总数。 例如
Category Name: Journalist
Total users: 10
答案 0 :(得分:2)
试试这个(集合应该是你拥有的集合)
var mapFunction = function() {
for (a in this.likes.data){
var name = this.likes.data[a].category;
emit(name, 1);
}
}
var reduceFunction = function(key, val){
return Array.sum(val);
}
db.collection.mapReduce(mapFunction, reduceFunction, {out: "myresult"});
db.myresult.find();