2016-08-23 46 views
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); 
    }); 
+0

嗨chridam,我添加了代码。谢谢! – arkanos

+0

你能够改变模式,使'queues'成为一个数组吗? – chridam

+0

是的,我将能够执行更改。 – arkanos

回答

2

如果你能调整你的架构遵循这一设计,例如填充测试收集与样品中的文件有这样的流体重新设计架构:

db.test.insert([ 
    {  
    "items": [ 
     { 
      "key": "BCNED3351", 
      "timestamp" : 1471873201170.0, 
      "totalOID" : { 
       "backlog" : 1405, 
       "inflow" : 396, 
       "handled" : 341 
      }, 
      "queues" : [ 
       { 
        "key": 12, 
        "backlog" : 5, 
        "inflow" : 0, 
        "handled" : 0 
       }, 
       { 
        "key": 30, 
        "backlog" : 124, 
        "inflow" : 1, 
        "handled" : 1 
       }, 
       { 
        "key": 31, 
        "backlog" : 15, 
        "inflow" : 40, 
        "handled" : 29 
       }, 
       { 
        "key": 33, 
        "backlog" : 1, 
        "inflow" : 12, 
        "handled" : 12 
       }, 
       { 
        "key": 36, 
        "backlog" : 285, 
        "inflow" : 38, 
        "handled" : 0 
       }, 
       { 
        "key": 40, 
        "backlog" : 1, 
        "inflow" : 1, 
        "handled" : 0 
       }, 
       { 
        "key": 42, 
        "backlog" : 968, 
        "inflow" : 268, 
        "handled" : 267 
       }, 
       { 
        "key": 44, 
        "backlog" : 5, 
        "inflow" : 35, 
        "handled" : 32 
       }, 
       { 
        "key": 68, 
        "backlog" : 1, 
        "inflow" : 1, 
        "handled" : 0 
       } 
      ] 
     } 
    ] 
}, 
{ 
    "items": [ 
     { 
      "key": "PARED3100", 
      "timestamp" : 1471873202167.0, 
      "totalOID" : { 
       "backlog" : 28, 
       "inflow" : 0, 
       "handled" : 0 
      }, 
      "queues" : [ 
       { 
        "key": 30, 
        "backlog" : 25, 
        "inflow" : 0, 
        "handled" : 0 
       }, 
       { 
        "key": 31, 
        "backlog" : 2, 
        "inflow" : 0, 
        "handled" : 0 
       }, 
       { 
        "key": 36, 
        "backlog" : 1, 
        "inflow" : 0, 
        "handled" : 0 
       } 
      ] 
     } 
    ] 
} 
]) 

然后你可以运行在聚集管道:

db.test.aggregate([ 
    { "$unwind": "$items" }, 
    { "$unwind": "$items.queues" }, 
    { 
     "$group": { 
      "_id": { 
       "item": "$items.key", 
       "queue": "$items.queues.key" 
      }, 
      "backlog": { "$sum": "$items.queues.backlog" }, 
      "inflow": { "$sum": "$items.queues.inflow" }, 
      "handled": { "$sum": "items.queues.handled" } 
     } 
    }  
]) 

并得到结果:

/* 1 */ 
{ 
    "_id" : { 
     "item" : "BCNED3351", 
     "queue" : 12 
    }, 
    "backlog" : 5, 
    "inflow" : 0, 
    "handled" : 0 
} 

/* 2 */ 
{ 
    "_id" : { 
     "item" : "BCNED3351", 
     "queue" : 30 
    }, 
    "backlog" : 124, 
    "inflow" : 1, 
    "handled" : 0 
} 

/* 3 */ 
{ 
    "_id" : { 
     "item" : "BCNED3351", 
     "queue" : 31 
    }, 
    "backlog" : 15, 
    "inflow" : 40, 
    "handled" : 0 
} 

/* 4 */ 
{ 
    "_id" : { 
     "item" : "BCNED3351", 
     "queue" : 33 
    }, 
    "backlog" : 1, 
    "inflow" : 12, 
    "handled" : 0 
} 

/* 5 */ 
{ 
    "_id" : { 
     "item" : "BCNED3351", 
     "queue" : 36 
    }, 
    "backlog" : 285, 
    "inflow" : 38, 
    "handled" : 0 
} 

/* 6 */ 
{ 
    "_id" : { 
     "item" : "BCNED3351", 
     "queue" : 40 
    }, 
    "backlog" : 1, 
    "inflow" : 1, 
    "handled" : 0 
} 

/* 7 */ 
{ 
    "_id" : { 
     "item" : "BCNED3351", 
     "queue" : 42 
    }, 
    "backlog" : 968, 
    "inflow" : 268, 
    "handled" : 0 
} 

/* 8 */ 
{ 
    "_id" : { 
     "item" : "BCNED3351", 
     "queue" : 44 
    }, 
    "backlog" : 5, 
    "inflow" : 35, 
    "handled" : 0 
} 

/* 9 */ 
{ 
    "_id" : { 
     "item" : "BCNED3351", 
     "queue" : 68 
    }, 
    "backlog" : 1, 
    "inflow" : 1, 
    "handled" : 0 
} 

/* 10 */ 
{ 
    "_id" : { 
     "item" : "PARED3100", 
     "queue" : 36 
    }, 
    "backlog" : 1, 
    "inflow" : 0, 
    "handled" : 0 
} 

/* 11 */ 
{ 
    "_id" : { 
     "item" : "PARED3100", 
     "queue" : 31 
    }, 
    "backlog" : 2, 
    "inflow" : 0, 
    "handled" : 0 
} 

/* 12 */ 
{ 
    "_id" : { 
     "item" : "PARED3100", 
     "queue" : 30 
    }, 
    "backlog" : 25, 
    "inflow" : 0, 
    "handled" : 0 
} 
+1

嗨,再次chridam,我编辑了模式,它现在与您的查询很好。 非常感谢! – arkanos