इसके लिए $lookup का उपयोग कर सकते हैं। आइए दस्तावेज़ों के साथ पहला संग्रह बनाएं:
> 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" : [ ] })
पहले संग्रह के सभी दस्तावेज़ प्रदर्शित करें
> db.demo446.find();
आउटपुट:
{ "_id" : ObjectId("5e790766bbc41e36cc3caec3"), "ProductName" : "Product1", "ProductPrice" : 60 } { "_id" : ObjectId("5e790766bbc41e36cc3caec4"), "ProductName" : "Product2", "ProductPrice" : 90 }
दूसरा संग्रह बनाएं
> 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" : [ ] })
दूसरे संग्रह के सभी दस्तावेज़ प्रदर्शित करें
> db.demo447.find();
आउटपुट:
{ "_id" : ObjectId("5e790789bbc41e36cc3caec5"), "ProductName" : "Product1", "ProductPrice" : 40 } { "_id" : ObjectId("5e790789bbc41e36cc3caec6"), "ProductName" : "Product2", "ProductPrice" : 70 }
दोनों संग्रहों को एग्रीगेट करने की क्वेरी (जहां पहला दूसरे से बड़ा हो)
> 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" ... ] ... } ... } ... ])
आउटपुट
{ "_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 } }