How to aggregate two collections where a field from one collection is greater than the other in MongoDB?


For this, you can use $lookup. Let us create a collection with documents −

> db.demo446.insert([
...    { "ProductName": "Product1", "ProductPrice": 60 },
...    { "ProductName": "Product2", "ProductPrice": 90 }
... ])
BulkWriteResult({
   "writeErrors" : [ ],
   "writeConcernErrors" : [ ],
   "nInserted" : 2,
   "nUpserted" : 0,
   "nMatched" : 0,
   "nModified" : 0,
   "nRemoved" : 0,
   "upserted" : [ ]
})

Display all documents from a collection with the help of find() method −

> db.demo446.find();

This will produce the following output −

{ "_id" : ObjectId("5e790766bbc41e36cc3caec3"), "ProductName" : "Product1", "ProductPrice" : 60 }
{ "_id" : ObjectId("5e790766bbc41e36cc3caec4"), "ProductName" : "Product2", "ProductPrice" : 90 }

Following is the query to create second collection with documents −

> db.demo447.insert([
...
...    { "ProductName": "Product1", "ProductPrice": 40 },
...    { "ProductName": "Product2", "ProductPrice": 70 }
... ])
BulkWriteResult({
   "writeErrors" : [ ],
   "writeConcernErrors" : [ ],
   "nInserted" : 2,
   "nUpserted" : 0,
   "nMatched" : 0,
   "nModified" : 0,
   "nRemoved" : 0,
   "upserted" : [ ]
})

Display all documents from a collection with the help of find() method −

> db.demo447.find();

This will produce the following output −

{ "_id" : ObjectId("5e790789bbc41e36cc3caec5"), "ProductName" : "Product1", "ProductPrice" : 40 }
{ "_id" : ObjectId("5e790789bbc41e36cc3caec6"), "ProductName" : "Product2", "ProductPrice" : 70 }

Following is the query to aggregate two collections where a field from one collection is greater than the other −

> var rate = 1;
> db.demo446.aggregate([
... { "$match": { "ProductPrice": { "$exists": true }, "ProductName": { "$exists": true } } },
... {
...    "$lookup": {
...       "from": "demo447",
...       "localField": "ProductName",
...       "foreignField": "ProductName",
...       "as": "demo447"
...    }
... },
... { "$unwind": "$demo447" },
... {
...    "$redact": {
...       "$cond": [
...          {
...             "$gt": [
...                "$ProductPrice", {
...                   "$add": [
...                         { "$multiply": [ "$demo447.ProductPrice",rate ] },
...                         3
...                      ]
...                   }
...                ]
...             },
...             "$$KEEP",
...             "$$PRUNE"
...          ]
...       }
...    }
... ])

This will produce the following output −

{ "_id" : ObjectId("5e790766bbc41e36cc3caec3"), "ProductName" : "Product1", "ProductPrice" : 60, "demo447" : { "_id" : ObjectId("5e790789bbc41e36cc3caec5"), "ProductName" : "Product1", "ProductPrice" : 40 } }
{ "_id" : ObjectId("5e790766bbc41e36cc3caec4"), "ProductName" : "Product2", "ProductPrice" : 90, "demo447" : { "_id" : ObjectId("5e790789bbc41e36cc3caec6"), "ProductName" : "Product2", "ProductPrice" : 70 } }

Updated on: 06-Apr-2020

395 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements