Node.js MongoDB单次查询中的多组

Node.js MongoDB单次查询中的多组,node.js,mongodb,Node.js,Mongodb,我有一组多条记录,有不同的“标签”和“目的” 我想先用“标签”id,然后用“目的”对我的记录进行分组,并显示如下结果: [ { "label" : "5c94fb7722ac2a0017e70857", "balance" : { "purchasing" : 240404, "deposit" : 4342 } }, { "label" : "5c94fb77

我有一组多条记录,有不同的“标签”和“目的”

我想先用“标签”id,然后用“目的”对我的记录进行分组,并显示如下结果:

[
    {
        "label" : "5c94fb7722ac2a0017e70857",
        "balance" : {
            "purchasing" : 240404,
            "deposit" : 4342
        }
    },
    {
        "label" : "5c94fb7722ac2rewt17e7084w58",
        "balance" : {
            "Purchasing" : 455356,
            "Deposit" : 56577
        }
    },
]

在过去,我使用了
groupBy
lodash func。见:

因此,以对象数组的形式构建记录集合:

const records = [{
    billNumber: "",
    _id: "5c94fbc222ac2a0017e70859",
    label: "5c94fb7722ac2a0017e70857",
    purpose: "Purchasing",
    date: "2019-03-27T00:00:00.000Z",
    amount: 200000
},
{
    billNumber: "",
    _id: "5c95e300a634360017498c39",
    label: "5c94fba222ac2a0017e70858",
    purpose: "Deposit",
    date: "2018-05-08T00:00:00.000Z",
    amount: 792
}]
然后调用Lodash方法
groupBy
作为
..groupBy(记录,'label')将返回:

$: { 5c94fb7722ac2a0017e70857: 
   [ { billNumber: '',
       _id: '5c94fbc222ac2a0017e70859',
       label: '5c94fb7722ac2a0017e70857',
       purpose: 'Purchasing',
       date: '2019-03-27T00:00:00.000Z',
       amount: 200000 } ],
  5c94fba222ac2a0017e70858: 
   [ { billNumber: '',
       _id: '5c95e300a634360017498c39',
       label: '5c94fba222ac2a0017e70858',
       purpose: 'Deposit',
       date: '2018-05-08T00:00:00.000Z',
       amount: 792 } ] }
下面是一个工作片段:

const记录=[{
号码:“,
_id:“5c94fbc222ac2a0017e70859”,
标签:“5c94fb7722ac2a0017e70857”,
目的:“采购”,
日期:“2019-03-27T00:00:00.000Z”,
金额:20万
},
{
号码:“,
_id:“5c95e300a634360017498c39”,
标签:“5c94fba222ac2a0017e70858”,
用途:“存款”,
日期:“2018-05-08T00:00:00.000Z”,
金额:792
}]
const grouped=u.groupBy(记录,'label');
console.log(分组)

你的问题是?到目前为止你写了什么代码?{$group:{“\u id”:{“label”:“$label”,“purpose”:“$purpose”,“amount”:“$amount”},},{$group:{“\u id”:“$\u id.purpose”,“amount”:{$sum:$\u id.amount”}
$: { 5c94fb7722ac2a0017e70857: 
   [ { billNumber: '',
       _id: '5c94fbc222ac2a0017e70859',
       label: '5c94fb7722ac2a0017e70857',
       purpose: 'Purchasing',
       date: '2019-03-27T00:00:00.000Z',
       amount: 200000 } ],
  5c94fba222ac2a0017e70858: 
   [ { billNumber: '',
       _id: '5c95e300a634360017498c39',
       label: '5c94fba222ac2a0017e70858',
       purpose: 'Deposit',
       date: '2018-05-08T00:00:00.000Z',
       amount: 792 } ] }