MongoDB不做JOIN。所以你将不得不查询这两个集合并在应用层上执行JOIN。如何做到这一点完全取决于您使用哪种编程语言来开发应用程序。你不会说你使用哪一个,所以我只给你一个JavaScript例子。当你使用不同的语言:第二个片段只是一个简单的FOR循环。
这些是您要使用的MongoDB查询。我无法访问您的数据,因此我无法保证正确性。
var reports = db.checks_reports_df8.find({
"started": {$exists: 1 },
"finished": {$exists: 0 }
});
此查询假定您的空值由缺少的字段表示,这是MongoDB中的常规做法。当您有实际的null
值时,请使用"started": { $ne: null }
和"finished": null
。
然后迭代您获得的文档数组。对于每个RESULT
执行此查询:
reports.forEach(function(report) {
var job_count = db.checks_jobs_df8.aggregate([
{$match: {
"report_id": report.id,
"is_done": 1
}},
{$group: {
_id: "$job_id",
"count": { $sum: 1 }
}}
])
// output the data from report and job_count here
});
谢谢@Philipp。这正是我需要的。顺便提一下,Python的语言是Python。但是很容易适应你的答案。谢谢! – 2014-11-25 10:15:43