go - $lookup and $match Mongodb golang -


i want document foreign key using $lookup , $match on mongodb.

there "jobs" collection stores job document. in job document there 2 field using foreing key "creatorparent" , "children". creatorparent foreign key "users" collection , children array contains id user's children.

when list whole jobs, want retrieve detail "users" collection both creatorparent id , childrenid. want marshall "job" document parentdetail , childdetail. don't want write custom method that. possible handle mongodb query?

by way i'm beginner on mongodb should store needed details on children , creatorparent instead of storing objectid?

jobs collection

users collection

users document:

{     "_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         }     ] } 

job

{     "_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" : [] } 

if understood correctly. similar solution achievable using mongodb 3.4's $addfields , $lookup aggregation steps.

mongo aggregation:

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

the output following:

{ "_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:

if substitute last $group stage this:

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

then can achieve to, in $group stage have specify every field of job one-by-one $first expression.


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -