2017-03-28 44 views
0

我想通過使用$查找和$匹配有關MongoDB獲得與國外關鍵文件。

有一個「喬布斯」收集存儲工作文檔。在工作文件中有兩個領域使用作爲主鍵「creatorParent」和「兒童」。 CreatorParent是「用戶」集合的外鍵,而Children數組包含用戶子項的ID。

當我列出了整個工作,我想要從「用戶」收集細節兩個CreatorParent ID和ChildrenID。我想用ParentDetail和ChildDetail編寫「Job」文檔。我不想爲此編寫自定義方法。是否有可能使用MongoDB查詢處理它?

順便說一句,我是MongoDB的初學者,所以應該在Children和CreatorParent上存儲需要的細節,而不是存儲ObjectId?

Jobs Collection

Users Collection

用戶文檔:

{ 
    "_id" : ObjectId("58daf84877733645eaa9b44f"), 
    "email" : "[email protected]", 
    "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" : [] 
} 

回答

0

如果我理解正確的話。使用MongoDB 3.4的$ addFields和$ lookup彙總步驟可以實現類似的解決方案。

蒙戈聚集:

[ 
    { 
     $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 
    } 
]} 

UPDATE:

如果替換最後$group階段,這樣的:

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

然後你就可以達到你想要什麼,但在這個階段$group,你必須指定作業中的一接一個的各個領域與$first表達。

+0

你好,謝謝你的回答,但我一直在尋找不同的東西,當我從數據庫,而不是創造者一方的身份證取回的工作,我想creatorParent的細節像你下面列出。像左連接上SQL –

+0

我試過你的代碼,但我無法檢索子詳細 –

+0

兒童領域仍返回空數組:( –