[英]Creating a MapReduce query in MongoDB
I have a slight problem. 我有一个小问题。 Until now, I'm not so deep in the Mongo DB stuff. 到目前为止,我对Mongo DB的了解还不深。 More like the SQL guy, but now I have to crate a Map Reduce query. 更像是SQL专家,但现在我必须创建一个Map Reduce查询。
My data looks like this: 我的数据如下所示:
Every student has multiple lectures. 每个学生都有多个讲座。 Now I want a list of all lectures (no duplicates) and the number of students visiting it. 现在,我想获得所有讲座的列表(无重复)以及访问它的学生人数。
function() {
emit(this.vorlesungen, this._id);
};
function(VL, students) {
return Array.sum(students);
};
This gives me some strange result I can´t figure out. 这给了我一些无法理解的奇怪结果。
Some hints would be great. 一些提示会很棒。
I'm not sure if this is the data that you're expecting but give it a try: 我不确定这是否是您期望的数据,但请尝试一下:
db.collection.aggregate([
{$unwind : "$vorlesungen"},
{$group : {_id : "$vorlesungen", studentCount: {$sum : 1}}}
])
I found a aulution by myself: 我自己发现了一个缺点:
Map: 地图:
function() { this.vorlesungen.forEach(function (elem) { for(vlname in elem) { emit(vlname, 1); } });
Reduce: 降低:
function(key, values) { return Array.sum(values); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.