簡體   English   中英

$ lookup和$ match Mongodb golang

[英]$lookup and $match Mongodb golang

我想通過在MongoDB上使用$ lookup和$ match獲得帶有外鍵的文檔。

有一個“作業”集合,用於存儲作業文檔。 在作業文檔中,有兩個字段用作先行鍵“ creatorParent”和“ Children”。 CreatorParent是“用戶”集合的外鍵,Children數組包含用戶子代的ID。

當我列出所有作業時,我想從CreatorsParent ID和ChildrenID的“用戶”集合中檢索詳細信息。 我想將“作業”文檔與ParentDetail和ChildDetail一起編組。 我不想為此編寫自定義方法。 有可能用MongoDB查詢來處理嗎?

順便說一下,我是MongoDB的初學者,所以應該在Children和CreatorParent上存儲所需的詳細信息,而不是存儲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
        }
    ]
}

工作

{
    "_id" : ObjectId("58db0a2d77733645eaa9b453"),
    "creationtime" : ISODate("2017-03-29T01:13:17.509Z"),
    "startingtime" : ISODate("2017-04-03T13:00:00.000Z"),
    "endingtime" : ISODate("2017-04-03T17:00:00.000Z"),
    "children" : [ 
        ObjectId("58daf84977733645eaa9b450"), 
        ObjectId("58daf84977733645eaa9b451")
    ],
    "creatorparent" : ObjectId("58daf84877733645eaa9b44f"),
    "applicants" : []
}

如果我理解正確的話。 使用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表達式逐一指定作業的每個字段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM