我正在处理以下文件
{
"_id" : 123344223,
"firstName" : "gopal",
"gopal" : [
{
"uuid" : "123",
"name" : "sugun",
"sudeep" : [
{
"uuid" : "add32",
"name" : "ssss"
},
{
"uuid" : "fdg456",
"name" : "gfg"
}
]
},
{
"uuid" : "222",
"name" : "kiran"
}
]
}
我希望我的输出如下
{
"_id" : 456,
"gopal" : [
{
"uuid" : "123",
"name" : "sugun",
"sudeep" : [
{
"uuid" : "add32",
"name" : "ssss"
}
]
}
]
}
我试过很多像
这样的事情db.People.findOne({_id:123},{gopal:{$elemMatch:{uuid:"123",sudeep:{$elemMatch:{uuid:"add32"}}}}});
但无论我尝试过什么,都会像这样返回文件
{
"_id" : 123,
"gopal" : [
{
"uuid" : "123",
"name" : "sugun",
"sudeep" : [
{
"uuid" : "add32",
"name" : "ssss"
},
{
"uuid" : "fdg456",
"name" : "gfg"
}
]
}
]
}
你可以帮忙吗?
答案 0 :(得分:2)
您是否了解MongoDB aggregation pipeline
?
> db.people.aggregate([
{$match: {_id: 123}},
{$unwind: "$gopal"},
{$unwind: "$gopal.sudeep"},
{$match: {"gopal.uuid": "123", "gopal.sudeep.uuid" : "add32"}}
])
输出
{
"_id" : 123,
"firstName" : "gopal",
"gopal" : {
"uuid" : "123",
"name" : "sugun",
"sudeep" : {
"uuid" : "add32",
"name" : "ssss"
}
}
}
答案 1 :(得分:0)
在聚合管道中使用$ unwind
db.People.aggregate([{$匹配:{_ ID: '123'}},{$匹配:{gopal.uuid: '123'}},{$展开: '$萨迪普'},{$匹配:{UUID: 'ADD32'}}])