दस्तावेज़ फ़ील्ड मानों से मान घटाने के लिए, MongoDB समुच्चय() में $subtract का उपयोग करें। आइए दस्तावेजों के साथ एक संग्रह बनाएं -
> db.demo599.insertOne({"TotalPrice":250,"DiscountPrice":35});{
"acknowledged" : true, "insertedId" : ObjectId("5e948192f5f1e70e134e2696")
}
> db.demo599.insertOne({"TotalPrice":400,"DiscountPrice":10});{
"acknowledged" : true, "insertedId" : ObjectId("5e948199f5f1e70e134e2697")
}
> db.demo599.insertOne({"TotalPrice":1550,"DiscountPrice":50});{
"acknowledged" : true, "insertedId" : ObjectId("5e9481a0f5f1e70e134e2698")
} संग्रह से सभी दस्तावेज़ों को खोजने () विधि की सहायता से प्रदर्शित करें -
> db.demo599.find();
यह निम्नलिखित आउटपुट देगा -
{ "_id" : ObjectId("5e948192f5f1e70e134e2696"), "TotalPrice" : 250, "DiscountPrice" : 35 }
{ "_id" : ObjectId("5e948199f5f1e70e134e2697"), "TotalPrice" : 400, "DiscountPrice" : 10 }
{ "_id" : ObjectId("5e9481a0f5f1e70e134e2698"), "TotalPrice" : 1550, "DiscountPrice" : 50 } दस्तावेज़ फ़ील्ड मानों से मानों को घटाने के लिए क्वेरी निम्नलिखित है -
> db.demo599.aggregate( [ { $project: {ActualPrice: { $subtract: [ "$TotalPrice", "$DiscountPrice" ] } } } ] ) यह निम्नलिखित आउटपुट देगा -
{ "_id" : ObjectId("5e948192f5f1e70e134e2696"), "ActualPrice" : 215 }
{ "_id" : ObjectId("5e948199f5f1e70e134e2697"), "ActualPrice" : 390 }
{ "_id" : ObjectId("5e9481a0f5f1e70e134e2698"), "ActualPrice" : 1500 }