Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> MongoDB

MongoDB में ऐरे में ऑब्जेक्ट को विशिष्ट की द्वारा अपडेट करना

MongoDB में पोजिशनल $ ऑपरेटर का उपयोग करके ऐरे में नेस्टेड ऑब्जेक्ट अपडेट करना

MongoDB में किसी ऐरे के भीतर विशिष्ट की के आधार पर ऑब्जेक्ट को खोजकर उसके फील्ड को अपडेट करने के लिए पोजिशनल $ ऑपरेटर का उपयोग करें।

उदाहरण: संग्रह और दस्तावेज़ बनाना

> db.demo419.insertOne({
  "ProductInformation":[
    {"ProductName":"Product-1","ProductPrice":500},
    {"ProductName":"Product-2","ProductPrice":600}
  ]
});
{ "acknowledged" : true, "insertedId" : ObjectId("5e724762b912067e57771ae8") }

दस्तावेज़ देखना

> db.demo419.find();

आउटपुट:

{ "_id" : ObjectId("5e724762b912067e57771ae8"), "ProductInformation" : [ { "ProductName" : "Product-1", "ProductPrice" : 500 }, { "ProductName" : "Product-2", "ProductPrice" : 600 } ] }

ProductName "Product-1" वाले ऑब्जेक्ट की कीमत अपडेट करना

> db.demo419.update(
  {'ProductInformation.ProductName' : "Product-1" },
  { $set : { 'ProductInformation.$.ProductPrice' : 1250 }}
);
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

अपडेट के बाद दस्तावेज़ देखना

> db.demo419.find();

आउटपुट:

{ "_id" : ObjectId("5e724762b912067e57771ae8"), "ProductInformation" : [ { "ProductName" : "Product-1", "ProductPrice" : 1250 }, { "ProductName" : "Product-2", "ProductPrice" : 600 } ] }

व्याख्या:

  • 'ProductInformation.ProductName' : "Product-1" - ऐरे में इस नाम वाला ऑब्जेक्ट खोजें
  • ProductInformation.$.ProductPrice - $ मेल खाने वाले ऐरे एलिमेंट की स्थिति को संदर्भित करता है
  • केवल पहला मेल खाने वाला एलिमेंट अपडेट होता है

एकाधिक मेल खाने वाले एलिमेंट्स अपडेट करने के लिए (MongoDB 3.6+):

db.demo419.updateMany(
  {},
  { $set: { "ProductInformation.$[elem].ProductPrice": 1250 } },
  { arrayFilters: [ { "elem.ProductName": "Product-1" } ] }
)

arrayFilters लाभ:

  • एक ही ऑपरेशन में सभी मेल खाने वाले एलिमेंट्स अपडेट
  • जटिल शर्तें समर्थित
  • बेहतर पठनीयता
  1. - MongoDB क्वेरी: इंडेक्स N पर ऐरे ऑब्जेक्ट अपडेट करें

    MongoDB में ऐरे ऑब्जेक्ट अपडेट करने के लिए update() का उपयोग करें। डॉट नोटेशन का उपयोग भी आवश्यक है। आइए दस्तावेज़ों के साथ एक संग्रह बनाएं: > db.demo489.insertOne( ... { ... ... ...    details : [{ ...       id : 101, ...       "Info1" : { ... &

  1. - MongoDB में सरणी ऑब्जेक्ट के साथ सरणी तत्व के मान को बढ़ाएं

    सरणी ऑब्जेक्ट के मान को बढ़ाने के लिए, $inc का उपयोग करें। आइए दस्तावेज़ों के साथ एक संग्रह बनाएं: >db.demo506.insertOne({details:[{id:1,Quantity:4},{id:2,Quantity:3},{id:3,Quantity:2},{id:4,Quantity:7}]}); { acknowledged : true, insertedId : ObjectId(5e882ed6987b6e0e9d18f576) } find() मेथड की मद

  1. - MongoDB में Array Elements के साथ Specific Document Fetch करना

    MongoDB में किसी specific document को fetch करने के लिए find() मेथड में dot notation का उपयोग करें। आइए documents के साथ एक collection बनाते हैं: > db.demo672.insertOne({Brand:[{CategoryName:Mobile,Name:Oppo}]}); { acknowledged : true, insertedId : ObjectId(5ea3ea9b04263e90dac943e5) } > db.dem