梅雨东风
梅雨东风
  • 发布:2024-06-04 00:05
  • 更新:2024-06-04 19:30
  • 阅读:292

聚合操作的group使用非_id字段名报错the field must be an accumulator object,使用_id作为属性名则不会报错

分类:uniCloud

1、聚合操作的group使用非_id字段名报错the field must be an accumulator object,使用_id作为属性名则不会报错。如何输入name不报错?2、使用addFields新增字段时,bill[0].name或bill[0]不会输出,使用bill.name会输出一个数组,如何输出字符串?
数据如下
[
{
"_id": "66536bdf421fe337fac447b7",
"bh": "20245270534510",
"bill": [
{
"_id": "66536bfd421fe337fac447d1",
"bh": "202405270603979",
"createTime": 1716743163995,
"isincome": false,
"name": "八宝粥",
"note": "",
"num": 3,
"status": true,
"time": 1716743161000,
"type": "66536bdf421fe337fac447b7",
"updateTime": 1716743163995
},
{
"_id": "66536c1fedf2361f136c9a92",
"bh": "202405270638453",
"createTime": 1716743198467,
"isincome": false,
"name": "八宝粥",
"note": "",
"num": 4,
"status": true,
"time": 1716656796000,
"type": "66536bdf421fe337fac447b7",
"updateTime": 1716743198467
}
],
"createTime": 1716743134526,
"name": "早餐",
"note": "",
"status": true,
"time": 1716743124000,
"updateTime": 1716743134526
},
{
"_id": "66536be804cae60efec4d00d",
"bh": "20245270543841",
"bill": [
{
"_id": "66536c0fedf2361f136c9a8d",
"bh": "202405270622779",
"createTime": 1716743182794,
"isincome": false,
"name": "大米饭",
"note": "",
"num": 10,
"status": true,
"time": 1716743178000,
"type": "66536be804cae60efec4d00d",
"updateTime": 1716743182794
}
],
"createTime": 1716743143855,
"name": "午餐",
"note": "",
"status": true,
"time": 1716743142000,
"updateTime": 1716743143855
}
]

代码如下

const dbCmd = db.command;  
const $ = db.command.aggregate;  
db.collection("billType").aggregate().lookup({  
    from: 'bill',  
    let: {  
        billType_id: '$_id'  
    },  
    pipeline: $.pipeline()  
        .match(dbCmd.expr(  
            $.eq(['$type', '$$billType_id'])  
        ))  
        .done(),  
    as: 'bill'  
}).group({  
    name:"$name"  
}).end()
2024-06-04 00:05 负责人:DCloud_uniCloud_VK 分享
已邀请:
DCloud_uniCloud_VK

DCloud_uniCloud_VK

要回复问题请先登录注册