1

I have one table membership and another is promo codes.

membership table:

/* 1 */
{
    "_id" : ObjectId("5b62e7050b13587e9febb8db"),
    "country_id" : "5a093507f1d45633844096ef",
    "type" : "Bronze",
    "price" : "30",
}
/* 2 */
{
    "_id" : ObjectId("5b6961175140b477032291b7"),
    "country_id" : "5a093507f1d45633844096ef",
    "type" : "Gold",
    "price" : "1000",
}....

promo code table :

{
    "_id" : ObjectId("5cfe23fd075be65883f6d921"),
    "final_status" : 1,
    "membership" : [ 
        ObjectId("5b62e7050b13587e9febb8db"), 
        ObjectId("5b6961175140b477032291b7"), 
        ObjectId("5b6961285140b477032291b8"), 
        ObjectId("5b7567dd5b874856981b53d3"), 
        ObjectId("5bba1c3794c6761db256edbc")
    ],
    "month" : [ 
        "1", 
        "3"
    ]
}

here, the promomcode table has a membership id. I want promo code data with membership join query.get promo code data with membership detail Please help me

Dipika Patel
  • 49
  • 1
  • 10
  • Possible duplicate of [$lookup on ObjectId's in an array](https://stackoverflow.com/questions/34967482/lookup-on-objectids-in-an-array) – Vikash_Singh Jun 11 '19 at 08:43

1 Answers1

1

Use $lookup for left outer join in mongodb

db.promocode.aggregate([{
  $lookup: {
    from: 'membership',
    localField: 'membership',
    foreignField: '_id',
    as: 'memberships'
  }
}, {
  $project: {
    membership: 0
  }
}])
Vikash_Singh
  • 1,856
  • 2
  • 14
  • 27