Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在mongodb中查找文档中的子元素_Mongodb_Mongodb Query_Aggregation Framework - Fatal编程技术网

在mongodb中查找文档中的子元素

在mongodb中查找文档中的子元素,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,我正在编写以下文档 { "_id" : 123344223, "firstName" : "gopal", "gopal" : [ { "uuid" : "123", "name" : "sugun", "sudeep" : [ { "uuid" : "add32", "name" : "ssss" },

我正在编写以下文档

{
"_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"
            }
        ]
    }
]
}
您能帮忙吗?

您知道MongoDB吗

输出

{
        "_id" : 123,
        "firstName" : "gopal",
        "gopal" : {
                "uuid" : "123",
                "name" : "sugun",
                "sudeep" : {
                        "uuid" : "add32",
                        "name" : "ssss"
                }
        }
}

在聚合管道中使用$unwind

聚合([{$match:{uuid:'123'},{$match:{gopal.uuid:'123'},{$unwind:'sudeep'},{$match:{uuid:'add32'}}])

的可能重复项
{
        "_id" : 123,
        "firstName" : "gopal",
        "gopal" : {
                "uuid" : "123",
                "name" : "sugun",
                "sudeep" : {
                        "uuid" : "add32",
                        "name" : "ssss"
                }
        }
}