sarp
sarp

Reputation: 3750

How to use variables with MongoDB $lookup

Let's say I have 3 collections, cars, bikes, vehicles.

cars collection as:

{
  {
    "_id": "car1",
    "carBrand": "Audi",
    "color": "blue"
  },
  {
    "_id": "car2",
    "carBrand": "BMW",
    "color": "white"
  }
}

bikes collection as:

{
  {
    "_id": "bike1",
    "bikeBrand": "Audi",
    "color": "red"
  },
  {
    "_id": "bike2",
    "carBrand": "BMW",
    "color": "white"
  }
}

and the vehicles collection actually just has references to cars and bikes collections as

{
  {
    "_id": "vehicle1",
    "vehicleType": "cars",
    "vehicleId": "car1"
  },
  {
    "_id": "vehicle2",
    "vehicleType": "cars",
    "vehicleId": "car2"
  },
  {
    "_id": "vehicle3",
    "vehicleType": "bikes",
    "vehicleId": "bike1"
  },
  {
    "_id": "vehicle4",
    "vehicleType": "bikes",
    "vehicleId": "bike2"
  },

}

I'd like to join the vehicles collection with cars and bikes collections. I tried to set "$vehicleType" as a variable to $lookup's from field. However it does not work as I expected. It simply does not join the tables. No errors.

db.collection.aggregate([{

  $lookup: {
      from: "$vehicleType",
      localField: "vehicleId",
      foreignField: "_id",
      as: "vehicleDetails"
  }

}]);

I was expecting to have a result something like this

{
  {
    "_id": "vehicle1",
    "vehicleType": "cars",
    "vehicleId": "car1",
    "vehicleDetails": {    
      "_id": "car1",
      "carBrand": "Audi",
      "color": ""
    }
  },
  {
    "_id": "vehicle2",
    "vehicleType": "cars",
    "vehicleId": "car2",
    "vehicleDetails":    {
      "_id": "car2",
      "carBrand": "BMW",
      "color": "white"

  },
  {
    "_id": "vehicle3",
    "vehicleType": "bikes",
    "vehicleId": "bike1",
    "vehicleDetails":    {
      "_id": "bike1",
      "bikeBrand": "Audi",
      "color": "red"
    }
  },
  {
    "_id": "vehicle4",
    "vehicleType": "bikes",
    "vehicleId": "bike2",
    "vehicleDetails":    {
      "_id": "bike2",
      "carBrand": "BMW",
      "color": "white"
    }
  },

}    

Upvotes: 3

Views: 4533

Answers (1)

Alex Blex
Alex Blex

Reputation: 37048

If cars and bikes don't have common IDs, you can sequentially lookup in separate arrays, then combine them with $setUnion:

db.vehicles.aggregate([
  {$lookup: {
      from: "cars",
      localField: "vehicleId",
      foreignField: "_id",
      as: "carDetails"
  }},
  {$lookup: {
      from: "bikes",
      localField: "vehicleId",
      foreignField: "_id",
      as: "bikeDetails"
  }},
  {$project: {
     vehicleType: 1,
     vehicleId: 1,      
     vehicleDetails:{$setUnion: [ "$carDetails", "$bikeDetails" ]}
  }},
  {$project: {
      carDetails:0,
      bikeDetails:0,
  }}
]);

Otherwise you will need to use $facet to filter vehicles by type before lookup:

db.vehicles.aggregate([
   {
     $facet: {
         "cars": [
            {$match: {"vehicleType": "cars"}},
            {$lookup: {
               from: "cars",
               localField: "vehicleId",
               foreignField: "_id",
               as: "vehicleDetails"
             }},
         ],
         "bikes": [
            {$match: {"vehicleType": "bikes"}},
            {$lookup: {
               from: "bikes",
               localField: "vehicleId",
               foreignField: "_id",
               as: "vehicleDetails"
             }}
         ]
     }
   },
   {$project: {all: {$setUnion: ["$cars", "$bikes"]}}},
   {$unwind: "$all"},
   {$replaceRoot: { newRoot: "$all" }}
])

Upvotes: 7

Related Questions