Two queries on different collections - MongoDB -


i have 2 collections in mongodb. first contains information football coaches , second contains data teams. example, document of coach collection:

 {          "_id" : objectid("556caaac9262ab4f14165fca"),          "name" : "luis",          "surname" : "enrique martinez garcia",          "age" : 45,          "date_of_birth" : {                  "day" : 8,                  "month" : 5,                  "year" : 1970          },          "place_of_birth" : "gijòn",          "nationality" : "spanish",          "preferred_formation" : "4-3-3 off",          "coached_team" : [                  {                          "team_id" : "bar.43",                          "in_charge" : {                                  "from" : "01/july/2014"                          },                          "matches" : 59                  },                  {                          "team_id" : "cel.00",                          "in_charge" : {                                  "from" : "9/june/2013",                                  "to" : "30/june/2014"                          },                          "matches" : 40                  },                  {                          "team_id" : "rom.01",                          "in_charge" : {                                  "from" : "7/june/2011",                                  "to" : "10/may/2012"                          },                          "matches" : 41                  } 

here document of team collection:

   {            "_id" : "bar.43",            "official_name" : "futbol club barcelona",            "country" : "spain",            "started_by" : {                    "day" : 28,                    "month" : 11,                    "year" : 1899            },            "stadium" : {                    "name" : "camp nou",                    "capacity" : 99354            },            "palmarès" : {                    "la liga" : 23,                    "copa del rey" : 27,                    "supercopa de espana" : 11,                    "uefa champions league" : 4,                    "uefa cup winners cup" : 4,                    "uefa super cup" : 4,                    "fifa club world cup" : 2            },            "uniform" : "blue , dark red"    } 

well, know mongo not support join between collections. suppose saved return of query on team collection in array called x. example:

var x = db.team.find({_id:"bar.43"}).toarray() 

now want use array x query coach collection , find coaches coached team id. tried in ways, don't work:

[1]  db.coach.aggregate([{$unwind:"$coached_team"},{$match:{"coached_team.team_id:"x[0]._id"}}])  [2] db.team.find({"x[0]._id":{$in:coached_team}}) 

p.s. looked similar questions in forum, , answers don't reply mine.
this, example, not work.

you need remove quotes " around variable x[0]._id. otherwise encoded string , content of variable not looked , filled in.

var x = db.team.find({_id:"bar.43"}).toarray(); db.coach.find({"coached_team.team_id":x[0]._id}); 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -