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
$lookup和$match Mongodb golang_Mongodb_Go - Fatal编程技术网

$lookup和$match Mongodb golang

$lookup和$match Mongodb golang,mongodb,go,Mongodb,Go,我想在MongoDB上使用$lookup和$match来获取带有外键的文档 有一个“作业”集合,用于存储作业文档。在工作文档中,有两个字段用作外键“creatorParent”和“Children”。 CreatorParent是“用户”集合的外键,子数组包含用户子数组的id 当我列出所有作业时,我想从CreatorParent ID和ChildrenID的“用户”集合中检索详细信息。我想用ParentDetail和ChildDetail处理“Job”文档。我不想为此编写自定义方法。是否可以使用

我想在MongoDB上使用$lookup和$match来获取带有外键的文档

有一个“作业”集合,用于存储作业文档。在工作文档中,有两个字段用作外键“creatorParent”和“Children”。 CreatorParent是“用户”集合的外键,子数组包含用户子数组的id

当我列出所有作业时,我想从CreatorParent ID和ChildrenID的“用户”集合中检索详细信息。我想用ParentDetail和ChildDetail处理“Job”文档。我不想为此编写自定义方法。是否可以使用MongoDB查询来处理它

顺便说一句,我是MongoDB的初学者,所以应该存储孩子和创建者家长所需的详细信息,而不是存储ObjectId吗?

用户文档:

{
    "_id" : ObjectId("58daf84877733645eaa9b44f"),
    "email" : "meto93@gmail.com",
    "password" : "vpGl+Fjnef616cRgNbCkwaFDpSI=",
    "passwordsalt" : "99397F4A9D3A499D96694547667E74595CE994D2E83345D6953EF866303E8B65",
    "children" : [ 
        {
            "_id" : ObjectId("58daf84977733645eaa9b450"),
            "name" : "Mert",
            "age" : 5,
            "additionalinformation" : "ilk cocuk",
            "creationtime" : ISODate("2017-03-28T23:56:56.952Z"),
            "userid" : ObjectId("58daf84877733645eaa9b44f"),
            "gender" : null
        }, 
        {
            "_id" : ObjectId("58daf84977733645eaa9b451"),
            "name" : "Sencer",
            "age" : 7,
            "additionalinformation" : "ikinci cocuk",
            "creationtime" : ISODate("2017-03-28T23:56:56.952Z"),
            "userid" : ObjectId("58daf84877733645eaa9b44f"),
            "gender" : null
        }
    ]
}
工作


如果我理解正确的话。使用MongoDB 3.4的$addFields和$lookup聚合步骤可以实现类似的解决方案

Mongo聚合:

[
    {
        $addFields: { 
            "job":"$$ROOT"
        }
    },
    {
        $unwind: {
            path : "$children"
        }
    },
    {
        $lookup: {
            "from" : "users",
            "localField" : "creatorParent",
            "foreignField" : "_id",
            "as" : "creatorParent"
        }
    },
    {
        $lookup: {
            "from" : "users",
            "localField" : "children",
            "foreignField" : "_id",
            "as" : "children"
        }
    },
    {
        $group: {
            "_id": "$_id",
            "job": { "$first": "$job" },
            "creatorParent" : { "$first" : "$creatorParent" },
            "children": { "$addToSet": {  $arrayElemAt: [ "$children", 0 ]  } }
        }
    }
]
输出将如下所示:

{ "_id" : ObjectId("58da9cb6340c630315348114"), 
"job" : {
    "_id" : ObjectId("58da9cb6340c630315348114"), 
    "name" : "Developer", 
    "creatorParent" : ObjectId("58da9c79340c630315348113"), 
    "children" : [
        ObjectId("58da9c6d340c630315348112"), 
        ObjectId("58da9c5f340c630315348111")
    ], 
    "hourly_rate" : 12.0, 
    "additional_information" : "other infos"
}, 
"creatorParent" : [
    {
        "_id" : ObjectId("58da9c79340c630315348113"), 
        "name" : "The Boss", 
        "age" : 40.0
    }
], 
"children" : [
    {
        "_id" : ObjectId("58da9c5f340c630315348111"), 
        "name" : "James", 
        "age" : 28.0
    }, 
    {
        "_id" : ObjectId("58da9c6d340c630315348112"), 
        "name" : "Andrew", 
        "age" : 26.0
    }
]}
更新:

如果将最后一个
$group
阶段替换为:

{
    "_id": "$_id",
    "name": { "$first": "$name" },
    "jobstatus": { "$first": "$jobstatus" },
    "hourlyrate": { "$first":"$hourlyrate" },
    "creatorparent" : { "$first" : "$creatorparent" },
    "children": { "$addToSet": {  $arrayElemAt: [ "$children", 0 ]  } }
}

然后你可以实现你想要的,但是在这个
$group
阶段,你必须用
$first
表达式一个接一个地指定作业的每个字段。

您好,谢谢您的回答,但是当我从数据库中检索作业而不是从创建者家长的ID中检索作业时,我看到了一些不同的东西,我想得到creatorParent的详细信息,就像下面列出的一样。像SQLL上的左连接一样,我尝试了您的代码,但无法检索子detailchildren字段仍然返回空数组:(我在另一个线程中回答:
{
    "_id": "$_id",
    "name": { "$first": "$name" },
    "jobstatus": { "$first": "$jobstatus" },
    "hourlyrate": { "$first":"$hourlyrate" },
    "creatorparent" : { "$first" : "$creatorparent" },
    "children": { "$addToSet": {  $arrayElemAt: [ "$children", 0 ]  } }
}