我的数据看起来像这样
{
"_id": ObjectId("57ef642fd4f27bb41b697314"),
"compCode": "0001",
"menuName": "Cash",
"menuDesc": "Cash Module",
"formName": "",
"search": "n",
"effStatus": "A",
"dispSeq": "20",
"subMenu1": [{
"menuName": "Cash Vouchers",
"menuDesc": "Vouchers",
"formName": "",
"search": "n",
"effStatus": "A",
"dispSeq": "10",
"subMenu2": {
"menuName": "Cash Voucher",
"menuDesc": "Maintain Cash Voucher",
"formName": "cavoucher",
"search": "n",
"effStatus": "A",
"dispSeq": "10"
}
}]
};
我已经在上面给出了我的数据这个结果我已经使用mongodb聚合查询实现了这个结果但我希望我的submenu2必须在数组中而不是对象这是可能的当我尝试通过这样做“subMenu1.subMenu2”对subMenu2进行分组时: {“$ addToSet”:“$ subMenu1”}它显示我无法使用的错误。在小组声明中,我怎么能这样做我的预期结果是
{
"_id": ObjectId("57ef642fd4f27bb41b697314"),
"compCode": "0001",
"menuName": "Cash",
"menuDesc": "Cash Module",
"formName": "",
"search": "n",
"effStatus": "A",
"dispSeq": "20",
"subMenu1": [{
"menuName": "Cash Vouchers",
"menuDesc": "Vouchers",
"formName": "",
"search": "n",
"effStatus": "A",
"dispSeq": "10",
"subMenu2": [{
"menuName": "Cash Voucher",
"menuDesc": "Maintain Cash Voucher",
"formName": "cavoucher",
"search": "n",
"effStatus": "A",
"dispSeq": "10"
}]
}]
};
和我的汇总查询
db.sys_mainmenu.aggregate([
{$unwind:"$subMenu1"},
{$unwind:"$subMenu1.subMenu2"},
{$match:{"subMenu1.subMenu2.formName":{"$in":"cavoucher","systranslate"]}}},
{ "$group": {
"_id": "$_id",
"compCode":{"$first":"$compCode"},
"menuName":{"$first":"$menuName"},
"menuDesc": {"$first":"$menuDesc"},
"formName": {"$first":"$formName"},
"search": {"$first":"$search"},
"effStatus":{"$first": "$effStatus"},
"dispSeq": {"$first":"$dispSeq"},
"subMenu1": { "$addToSet": "$subMenu1" }
}
}
])