1

I need to join more than two fields in two collections using aggregate $lookup. is it possible to join? please let me know if it is possible. Here i have two collections:

For Example: "people" collections fields "city,state,country" in "country" collection fields "city_id,state_id,country_id", I want to join this three fields in following collections.

"People"

    {    
        "_id" : 1,
        "email" : "[email protected]",
        "userId" : "AD",
        "userName" : "admin",
        "city" : 1,
        "state" : 1,
        "country" : 1  
    }

"country"

     {
        "country_id" : 1,
        "userId" : "AD",
        "phone" : "0000000000",
        "stateinfo":[{
           "state_id" : 1,
           "state_name" : "State1"
         },{
           "state_id" : 2,
           "state_name" : "State2"
         }
         ],
        "cityinfo":[{
           "city_id" : 1,
           "city_name" : "city1"
         },{
           "city_id" : 2,
           "city_name" : "city2" 
         }
         ]
     }
1
  • Are these collections in PHP or mongo ?
    – Chris
    Mar 29, 2016 at 7:30

1 Answer 1

2

This is probably a lot more simple than you think, considering that of course all of the "three" fields are contained within the one "country" document. So it's just a matter of doing the $lookup by "country_id" and then using the retrived content to populate the other fields.

var pipeline = [
  { "$lookup": {
    "from": "country",
    "localField": "country",
    "foreignField": "country_id",
    "as": "country"
  }},
  { "$project": {
    "email": 1,
    "userId": 1,
    "userName": 1,
    "country": {
      "$arrayElemAt": [
        { "$filter": {
          "input": { 
            "$map": {
              "input": "$country",
              "as": "country",
              "in": {
                "country_id": "$$country.country_id",
                "userId": "$$country.userId",
                "phone": "$$country.phone",
                "stateInfo": {
                  "$arrayElemAt": [
                    { "$filter": {
                      "input": "$$country.stateInfo",
                      "as": "state",
                      "cond": { "$eq": [ "$$state.state_id", "$state" ] }
                    }},
                    0
                  ]
                },
                "cityinfo": {
                  "$arrayElemAt": [
                    { "$filter": {
                      "input": "$$country.cityinfo",
                      "as": "city",
                      "cond": { "$eq": [ "$$city.city_id", "$city" ] }
                    }},
                    0
                  ]
                }
              }
            }
          },
          "as": "country",
          "cond": { "$eq": [ "$$country.userId", "$userId" ] }
        }},
        0
      ]
    }
  }}
]

db.people.aggregate(pipeline)

That should give you a result like:

{    
  "_id" : 1,
  "email" : "[email protected]",
  "userId" : "AD",
  "userName" : "admin",
  "country" : {
    "country_id" : 1,
    "userId" : "AD",
    "phone" : "0000000000",
    "stateinfo": {
       "state_id" : 1,
       "state_name" : "State1"
    },
    "cityinfo": {
       "city_id" : 1,
       "city_name" : "city1"
    }
}

So once the array is matched in by $lookup it all comes down to using $filter to do the matcing and $arrayElemAt to get the first match from each filtered array.

Since the outer array has "inner" arrays, you want to use $map for the "outer" source and apply $filter to each of it's "inner" arrays.

You can get more fancy with $let to get that "reduced" array content down to the returned sub-document and then just directly reference the resulting properties for an even "flatter" response, but the general concept of "matching" the array elements remains the same as above.

For a PHP structure translation:

$pipeline = array(
  array(
    '$lookup' => array(
      'from' => 'country',
      'localField' => 'country'
      'foreignField' => 'country_id',
      'as' => 'country'
    )
  )
  array(
    '$project' => array(
      'email' => 1,
      'userId' => 1,
      'userName' => 1,
      'country' => array(
        '$arrayElemAt' => array(
          array(
            '$filter' => array(
              'input' => array(
                '$map' => array(
                  'input' => '$country',
                  'as' => 'country',
                  'in' => {
                    'country_id' => '$$country.country_id',
                    'userId' => '$$country.userId',
                    'phone' => '$$country.phone',
                    'stateInfo' => array(
                      '$arrayElemAt' => array(
                        array(
                          '$filter' => array(
                            'input' => '$$country.stateInfo',
                            'as' => 'state',
                            'cond' => array( '$eq' => array( '$$state.state_id', '$state' ) )
                          )
                        ),
                        0
                      )
                    ),
                    'cityinfo' => array(
                      '$arrayElemAt' => array(
                        array(
                          '$filter' => array(
                            'input' => '$$country.cityinfo',
                            'as' => 'city',
                            'cond' => array( '$eq' => array( '$$city.city_id', '$city' ) )
                          )
                        ),
                        0
                      )
                    )
                  }
                )
              ),
              'as' => 'country',
              'cond' => array( '$eq' => array( '$$country.userId', '$userId' ) )
            )
          ),
          0
        )
      )
    )
  )
);

$people->aggregate($pipeline);

You can usually check your PHP matches a JSON structure when you are working from a JSON example by dumping the pipeline structure:

echo json_encode($pipeline, JSON_PRETTY_PRINT)

And that way you cannot go wrong.

As another final note here, the process after the $lookup is done is quite "complex" even if very efficient. So I would advise that unless there is some need to take this aggregation pipeline further and actually "aggregate" something, then you are probably better off doing that "filtering" in client code rather than doing it on the server.

The client code to do the same thing is far less "obtuse" than what you need to tell the aggregation pipeline to do. So unless this "really" saves you a lot of bandwidth usage by reducing down the matched array, or indeed you if can just "lookup" by doing another query instead, then stick with doing it in code and/or do the seperate query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.