我有一个看起来像这样的集合:
db.GE_qstat_job_monitor.findOne()
{
"_id" : ObjectId("53fdf4bbe4b08d3f98fd6bcf"),
"JB_job_number" : NumberLong(45759),
"JB_script_file" : "/opt/UGE-8.1.7/examples/jobs/sleeper.sh",
"JB_owner" : "root",
"JB_job_name" : "Sleeper",
"job_state" : "Completed",
"submission_time" : ISODate("2014-08-26T22:27:51Z"),
"end_time" : ISODate("2014-08-27T15:17:49.564Z"),
"wall_clock" : NumberLong(60598),
"JB_pe" : "smp"
}
我需要通过" JB_owner"对每个文档进行分组。然后通过" job_state" (因为每个所有者可能有几个不同州的工作),然后计算每个所有者每个州的工作数量。
我在stackoverflow中找到了另一个问题,但没有任何帮助或暗示会非常感激: - )
答案 0 :(得分:1)
假设您的馆藏名为Jobs
,这应该有效:
var DEFAULT_DATA = {
Completed: 0,
Suspended: 0,
Running: 0,
Queue: 0,
cpu: 0,
wall_clock: 0
};
var jobs = Jobs.find().fetch();
var dataByOwner = {};
_.each(jobs, function(job) {
if (dataByOwner[job.JB_owner] == null)
dataByOwner[job.JB_owner] = _.clone(DEFAULT_DATA);
dataByOwner[job.JB_owner][job.job_state] += 1;
dataByOwner[job.JB_owner].wall_clock += job.wall_clock || 0;
dataByOwner[job.JB_owner].cpu += job.cpu || 0;
});
var result = _.map(dataByOwner, function(data, owner) {
var cpu = data.cpu;
var wall_clock = data.wall_clock;
delete data.cpu;
delete data.wall_clock;
return {
JB_owner: owner,
job_state: data,
cpu: cpu,
wall_clock: wall_clock
};
});
console.log(result);