Ashok
Ashok

Reputation: 21

Unable to perform left outer join using mongoDB Script

How do I perform a left outer join while accessing data from multiple collections? I want to get the details from collection even if there is no match in DB_Market. Presently if I filter further using condition, I am able to achieve inner join. However I also need left outer in addition to this.

Sample code

 db.new.aggregate([
 { 
    "$lookup": {
                "from": "DB_Market",
                "localField": "var1",
                "foreignField": "var1",
                "as": "collection2_doc"
           }
 },

 {"$unwind": "$collection2_doc" }])

Upvotes: 0

Views: 83

Answers (1)

Murugan Perumal
Murugan Perumal

Reputation: 985

I think you can perform left outer join. When you are doing $unwind, make sure preserveNullAndEmptyArrays is true

    db.new.aggregate([
     { 
        $lookup: {
                    "from": "DB_Market",
                    "localField": "var1",
                    "foreignField": "var1",
                    "as": "collection2_doc"
               }
     },

     {$unwind: { path: "$collection2_doc", preserveNullAndEmptyArrays: true }  }

     ])

Upvotes: 1

Related Questions