Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.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,我有三个收藏订户,地址,语言 Subscribersschema有以下记录 { "_id" : ObjectId("5a73fae80290f7eca89e99d4"), "FirstName" : "Rahul", "LastName" : "Shah", "Address" : DBRef("Address", ObjectId("5a6ec9bda3baf2b516de5769")),

我有三个收藏
订户
地址
语言

Subscribers
schema有以下记录

{
        "_id" : ObjectId("5a73fae80290f7eca89e99d4"),
        "FirstName" : "Rahul",
        "LastName" : "Shah",            
        "Address" : DBRef("Address", ObjectId("5a6ec9bda3baf2b516de5769")),            
        "Languages" : [
                DBRef("Languages", ObjectId("5a6304ffc3c3f119fc0e60c9")),
                DBRef("Languages", ObjectId("5a6ee970a3baf2b516de576b"))
        ]
}
{
        "_id" : ObjectId("5a6ec9bda3baf2b516de5769"),
        "Address1" : "Vastrapur,
        "Address2" : "Satellite",
        "City" : "Ahmedabad",
        "Country" : "India",
        "State" : "Gujarat",
        "ZipCode" : "380015",
        "PhoneNumber" : "(987)654-3210",
        "FaxNumber" : ""
}
{
        "_id" : ObjectId("5a6304ffc3c3f119fc0e60c9"),
        "Name" : "English",
        "LanguageCulture" : "English",
        "IsDeleted" : false
}
{
        "_id" : ObjectId("5a6ee970a3baf2b516de576b"),
        "Name" : "Hindi",
        "LanguageCulture" : "Hindi",
        "IsDeleted" : false
}
地址
架构有以下记录

{
        "_id" : ObjectId("5a73fae80290f7eca89e99d4"),
        "FirstName" : "Rahul",
        "LastName" : "Shah",            
        "Address" : DBRef("Address", ObjectId("5a6ec9bda3baf2b516de5769")),            
        "Languages" : [
                DBRef("Languages", ObjectId("5a6304ffc3c3f119fc0e60c9")),
                DBRef("Languages", ObjectId("5a6ee970a3baf2b516de576b"))
        ]
}
{
        "_id" : ObjectId("5a6ec9bda3baf2b516de5769"),
        "Address1" : "Vastrapur,
        "Address2" : "Satellite",
        "City" : "Ahmedabad",
        "Country" : "India",
        "State" : "Gujarat",
        "ZipCode" : "380015",
        "PhoneNumber" : "(987)654-3210",
        "FaxNumber" : ""
}
{
        "_id" : ObjectId("5a6304ffc3c3f119fc0e60c9"),
        "Name" : "English",
        "LanguageCulture" : "English",
        "IsDeleted" : false
}
{
        "_id" : ObjectId("5a6ee970a3baf2b516de576b"),
        "Name" : "Hindi",
        "LanguageCulture" : "Hindi",
        "IsDeleted" : false
}
语言
架构有以下记录

{
        "_id" : ObjectId("5a73fae80290f7eca89e99d4"),
        "FirstName" : "Rahul",
        "LastName" : "Shah",            
        "Address" : DBRef("Address", ObjectId("5a6ec9bda3baf2b516de5769")),            
        "Languages" : [
                DBRef("Languages", ObjectId("5a6304ffc3c3f119fc0e60c9")),
                DBRef("Languages", ObjectId("5a6ee970a3baf2b516de576b"))
        ]
}
{
        "_id" : ObjectId("5a6ec9bda3baf2b516de5769"),
        "Address1" : "Vastrapur,
        "Address2" : "Satellite",
        "City" : "Ahmedabad",
        "Country" : "India",
        "State" : "Gujarat",
        "ZipCode" : "380015",
        "PhoneNumber" : "(987)654-3210",
        "FaxNumber" : ""
}
{
        "_id" : ObjectId("5a6304ffc3c3f119fc0e60c9"),
        "Name" : "English",
        "LanguageCulture" : "English",
        "IsDeleted" : false
}
{
        "_id" : ObjectId("5a6ee970a3baf2b516de576b"),
        "Name" : "Hindi",
        "LanguageCulture" : "Hindi",
        "IsDeleted" : false
}
我希望输出如下

{
    "_id" : ObjectId("5a73fae80290f7eca89e99d4"),
    "FirstName" : "Rahul",
    "LastName" : "Shah",
    "Address" : {
        "_id" : ObjectId("5a6ec9bda3baf2b516de5769"),
        "Address1" : "Vastrapur",
        "Address2" : " Satellite ",
        "City" : " Ahmedabad ",
        "Country" : " India ",
        "State" : " Gujarat ",
        "ZipCode" : " 380015 ",
        "PhoneNumber" : "(987)654 - 3210 ",
        "FaxNumber" : " "
    },
    "Languages" : [{
            "_id" : ObjectId(" 5a6304ffc3c3f119fc0e60c9 "),
            " Name" : " English ",
            "LanguageCulture" : " English ",
            "IsDeleted" : false
        }, {
            "_id" : ObjectId(" 5a6ee970a3baf2b516de576b "),
            "Name" : " Hindi ",
            "LanguageCulture" : " Hindi ",
            "IsDeleted" : false
        }
    ]
}
为此,我做如下聚合

db.Subscribers.aggregate([{
            $project : {
                "FirstName" : 1,
                "LastName" : 1,
                Address : {
                    $let : {
                        vars : {
                            refParts : {
                                $objectToArray : "$$ROOT.Address"
                            }
                        },
                        in : "$$refParts"
                    }
                }
            }
        }, {
            $match : {
                "addressRefs" : {
                    $exists : true
                }
            }
        }, {
            $project : {
                "FirstName" : 1,
                "LastName" : 1,             
                "addressRefs" : {
                    $arrayElemAt : ["$addressRefs", 1]
                }
            }
        }, {
            $lookup : {
                from : "Addresses",
                localField : "addressRefs",
                foreignField : "_id",
                as : "address_data"
            }
        }, {
            $project : {
                "FirstName" : 1,
                "LastName" : 1,             
                "AddressUuid" : {
                    $arrayElemAt : ["$address_data.uuid", 0]
                }
            }
        }
    ])

给你。因为您使用了
DBRef
s,所以它并不漂亮,但由于以下答案,它完成了任务:


到目前为止,您能给我们看一下您的代码吗?对于Address字段,由于它不是数组,您需要将查询更改为$addFields:{“Address”:{$arrayElemAt:[{$objectToArray:“$Address”},1]},我已经编辑了您的答案,希望您不介意。