我创造了一个集“的帖子”有标题,描述,通过评论MongoDB中的各个用户在MongoDB中创造的职位数目:写MapReduce的函数来计算
db.posts.insert({
title:'MongoDB',
description:'MongoDB is a NoSQL DB',
by:'Tom',
comments:[
{user:'ram',
message:'We use MongoDB'
}
]
}
)
同样,我添加了另外两个条目。 现在,我想写MapReduce函数来统计MongoDB中各种用户创建的帖子数。我用:
db.posts.mapReduce(
function() { emit(this.user_id,1); },
function(key, values) {return Array.sum(values)}, {
out:"post_total"
}
).find()
输出:
{"id": null , "value": 3}
但是,我想显示是这样的:
{ "_id" : "tom_id", "value" : 2 }
{ "_id" : "mark_id", "value" : 1 }
或
{ "by" : "tom", "value" : 2 }
{ "by" : "mark", "value" : 1 }
db.posts.mapReduce( 函数(){ EMIT(this.user_id,1);} , 功能(键,值){ 返回Array.sum(值); },{ “出 “:{ ”内联“:1}} ).find() 输出: [{” ID“:NULL, ”值“:3}] 不工作。 –
@SarojShrestha为什么要将find()添加到内联输出中? – chridam
没有查找()输出 “结果”:[ { “ID”:空, “值”:3 } ] “timeMillis”:15, “计数”:{ “输入”:3, “EMIT”:3, “减少”:3, “输出”:1 }, “OK”:1 } –