Q:

How to find Intersection of Two Collection in monogdb?

Let say, I have 2 collection 
first one :-

db.product_main  

{
  _id:123121,
  source_id:"B4456dde1",
  title:"test Sample",
  price: 250
  quantity: 40
}

which consist approx ~10000 objects (Array) and unique field is source_id.
Second :-

db.product_id

{
  "_id":58745633,
  "product_id":"B4456dde1"
}

which consist of ~500 and only have field "product_id" which is equals to "source_id" of db.product_main
now, i want to intersect two collection so that i only find those which don't exist in db.product_id.

db.product_main.aggregate({any query})

A:

Just use the lookup stage to find the products associated with the 'product_main' collection and then match for empty array (i.e. records where no product_id was found)
db.product_main.aggregate([
  {
    $lookup: {
      from: "product_id",
      localField: "source_id",
      foreignField: "product_id",
      as: "products_available"
    }
  },
  {
    $match: {
      products_available: {
        $size: 0
      }
    }
  }
])

