假设Mongo中有数据! 有几个项目,还有管理者,分配问题的人。 每个问题都分配给不同的人,因为项目有不同的部分。 如何计算数字,每个受让人都已在项目中分配。 例如:
{ "Project A": { "Ted":10, "Matt":5 }, "Project B": { "Mary":3, "Hector":30 } }以下内容写入总和,但不对其进行分组:
db.test.issues.group({ "key": { "project": true "assignee":true }, "initial": { "countassignee": 0 }, "reduce": function(obj, prev) { prev.countassignee++; } });它将返回:
{ { "project": "Project A", "countassignee":10, "assignee":"Ted" } .... }Assume there's a data in Mongo! There are a few projects, and there're managers, to who the problems are assigned. Each problem is assigned to different people as the project has different parts. How can one compute the number, each assignee has been assigned within a project. For example:
{ "Project A": { "Ted":10, "Matt":5 }, "Project B": { "Mary":3, "Hector":30 } }The following writes the sum but doesn't group it:
db.test.issues.group({ "key": { "project": true "assignee":true }, "initial": { "countassignee": 0 }, "reduce": function(obj, prev) { prev.countassignee++; } });It will return as:
{ { "project": "Project A", "countassignee":10, "assignee":"Ted" } .... }最满意答案
您提供的数据样本非常令人困惑。 它的项目A和项目B应该是不同的文件。 我相信组的最佳用途是在集合中进行集合,如果你想在doucment中计算,你可以使用javascript。
OK! It doesn't seem like the best answer, but it'll do! first define a cursor sorting by projects
var cursor = db.test.find().sort({projects:1});a list for the counter:
var asgn_nums = {};and a variable holding project name;:
var project = '0';Here counts for each project:
while (cursor.hasNext()) { var issue = cursor.next(); var p = issue.project; if(p != project) { if(project != '0'){ print(project+":"); printjson(asgn_nums); } project = p; asgn_nums = {}; } if(issue.assignee != null) { if (asgn_nums[issue.assignee] == null) asgn_nums[issue.assignee] = 1; else asgn_nums[issue.assignee]+= 1; } if(!cursor.hasNext()) { print(project+":"); printjson(asgn_nums); } }更多推荐
发布评论