1
鉴于以下集合:MongoDB的收藏总金额多个对象
{
"_id" : ObjectId("57bb00b1471bcc08e819bff3"),
"BCNED3351" : {
"timestamp" : 1471873201170.0,
"totalOID" : {
"backlog" : 1405,
"inflow" : 396,
"handled" : 341
},
"queues" : {
"12" : {
"backlog" : 5,
"inflow" : 0,
"handled" : 0
},
"30" : {
"backlog" : 124,
"inflow" : 1,
"handled" : 1
},
"31" : {
"backlog" : 15,
"inflow" : 40,
"handled" : 29
},
"33" : {
"backlog" : 1,
"inflow" : 12,
"handled" : 12
},
"36" : {
"backlog" : 285,
"inflow" : 38,
"handled" : 0
},
"40" : {
"backlog" : 1,
"inflow" : 1,
"handled" : 0
},
"42" : {
"backlog" : 968,
"inflow" : 268,
"handled" : 267
},
"44" : {
"backlog" : 5,
"inflow" : 35,
"handled" : 32
},
"68" : {
"backlog" : 1,
"inflow" : 1,
"handled" : 0
}
}
}
}
/* 2 */
{
"_id" : ObjectId("57bb00b2471bcc08e819bff4"),
"PARED3100" : {
"timestamp" : 1471873202167.0,
"totalOID" : {
"backlog" : 28,
"inflow" : 0,
"handled" : 0
},
"queues" : {
"30" : {
"backlog" : 25,
"inflow" : 0,
"handled" : 0
},
"31" : {
"backlog" : 2,
"inflow" : 0,
"handled" : 0
},
"36" : {
"backlog" : 1,
"inflow" : 0,
"handled" : 0
}
}
}
}
我试图计算所有积压,流入之和为处理集合中的每个对象的每个队列元素。到目前为止,这是我想出了一个特定的队列不sucess:
var collection = db.collection('2016-08-23');
collection.aggregate([
{
$group:{
queue:'30',
backlog:
{
$sum:{$add:['$BCNED3351.queues.30.backlog','$PARED3100.queues.30.backlog']}
},
inflow:
{
$sum:{$add:['$BCNED3351.queues.30.inflow','$PARED3100.queues.30.inflow']}
},
handled:
{
$sum:{$add:['$BCNED3351.queues.30.handled','$PARED3100.queues.30.handled']}
}
}
}
], function(err, result) {
console.log(result);
});
似乎未找到该add函数的第二个参数,得到一个未定义的错误。考虑到队列的数量并不总是相同,对所有队列元素进行迭代并为每个对象集合中的所有对象创建一个总和的最好方法是什么?
有了这个代码,我能够在同一时间执行至少一个队列:
var collection = db.collection('2016-08-23');
collection.aggregate([
{
$group:{
_id:'30',
backlog:
{
$sum:'$BCNED3351.queues.30.backlog'
},
inflow:
{
$sum:'$BCNED3351.queues.30.inflow'
},
handled:
{
$sum:'$BCNED3351.queues.30.handled'
}
}
}
], function(err, result) {
console.log(result);
});
嗨chridam,我添加了代码。谢谢! – arkanos
你能够改变模式,使'queues'成为一个数组吗? – chridam
是的,我将能够执行更改。 – arkanos