我想创建基于滚动的菜单,如果有人有权查看 cavoucher,puvendor 形式,我知道只有表单名称,而且表单名称可以是文档中定义的任何位置,以及如何我可以得到结果只有匹配的内容和父母
[ {
"_id" : ObjectId("57ef642fd4f27bb41b697314"),
"menuDesc" : "Cash Module",
"formName" : "",
"subMenu1" : [
{
"menuDesc" : "Setup",
"formName" : "",
"subMenu2" : [
{
"menuDesc" : "Setup Bank Details",
"formName" : "cabank",
}
]
},
{
"menuDesc" : "Vouchers",
"formName" : "",
"subMenu2" : [
{
"menuDesc" : "Maintain Cash Voucher",
"formName" : "cavoucher",
},
{
"menuName" : "Paid Voucher",
"menuDesc" : "Maintain Paid Voucher",
"formName" : "capaidvchr",
}
]
}
]
},
{
"_id" : ObjectId("57ef6464d4f27bb41b697316"),
"menuDesc" : "Purchase Module",
"formName" : "",
"subMenu1" : [
{
"menuDesc" : "Maintain Vendors",
"formName" : "puvendor"
}
]
}]
我正在寻找的结果
[{
"_id" : ObjectId("57ef642fd4f27bb41b697314"),
"menuDesc" : "Cash Module",
"formName" : "",
"subMenu1" : [
{
"menuDesc" : "Vouchers",
"formName" : "",
"subMenu2" : [
{
"menuDesc" : "Maintain Cash Voucher",
"formName" : "cavoucher",
}
]
}
]
},{
"_id" : ObjectId("57ef6464d4f27bb41b697316"),
"menuDesc" : "Purchase Module",
"formName" : "",
"subMenu1" : [
{
"menuDesc" : "Maintain Vendors",
"formName" : "puvendor"
}
]
}}
我不知道哪个级别的值匹配,请建议任何可能的解决方案?
答案 0 :(得分:0)
可以使用MongoDB中的aggregation pipeline来完成。
在您的情况下,您有两个嵌套数组subMenu1
和subMenu2
首先$unwind,然后使用$match来获得结果。
Mongo Shell查询
db.collection.aggregate([
{$unwind:"$subMenu1"},
{$unwind:"$subMenu1.subMenu2"},
{$match:{"subMenu1.subMenu2.formName":"cavoucher"}}
])