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

MongoDB में एकल सरणी तत्व से एट्रिब्यूट अनसेट करना ($unset)

स्थितीय $ के साथ $unset

किसी सरणी के **विशिष्ट तत्व** से फील्ड हटाने (अनसेट) के लिए, अपडेट क्वेरी में मिलान की शर्त निर्दिष्ट करें, फिर $unset ऑपरेटर में स्थितीय $ प्लेसहोल्डर का उपयोग करें।

उदाहरण: डेटा डालना

> db.unsetAnAttributeDemo.insertOne(
... {
...   _id: 1,
...   "StudentDetails": [
...     { "StudentFirstName": "Ramit", "StudentCountryName": "UK" },
...     { "StudentFirstName": "Bob", "StudentCountryName": "US" },
...     { "StudentFirstName": "Carol", "StudentCountryName": "AUS" }
...   ]
... }
... );
{ "acknowledged" : true, "insertedId" : 1 }

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

> db.unsetAnAttributeDemo.find().pretty();

आउटपुट:


StudentCountryName "AUS" वाले तत्व से फील्ड अनसेट करना

> db.unsetAnAttributeDemo.update(
...   { "StudentDetails.StudentCountryName": "AUS" },
...   { $unset: { "StudentDetails.$.StudentCountryName": 1 } }
... );
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

परिणाम देखना

> db.unsetAnAttributeDemo.find().pretty();

आउटपुट (Carol के ऑब्जेक्ट से StudentCountryName गायब):


व्याख्या: क्वेरी { "StudentDetails.StudentCountryName": "AUS" } दस्तावेज़ और सरणी के अंदर विशिष्ट तत्व (तीसरा) से मेल खाती है। अपडेट में StudentDetails.$.StudentCountryName उस मिलान वाले तत्व को संदर्भित करता है। $unset उस फील्ड को पूरी तरह से हटा देता है।

  1. - MongoDB में सब प्रॉपर्टी के रूप में सरणी से तत्व कैसे हटाएं?

    MongoDB में नेस्टेड ऑब्जेक्ट के अंदर सरणी से विशिष्ट तत्व हटाने के लिए, डॉट नोटेशन के साथ $pull ऑपरेटर का उपयोग करें। उदाहरण: नेस्टेड उपयोगकर्ता जानकारी सरणी वाला दस्तावेज़ > db.demo388.insertOne({ _id: 101, userDetails: { isMarried: false, userInfo: [ { Name:

  1. - MongoDB में इंडेक्स द्वारा ऐरे परिणामों से एकल तत्व प्राप्त करना

    MongoDB में एग्रीगेशन का उपयोग करके ऐरे से एकल तत्व प्राप्त करना MongoDB में ऐरे परिणामों से किसी विशिष्ट इंडेक्स पर स्थित तत्व को प्राप्त करने के लिए एग्रीगेशन पाइपलाइन में $skip और $limit ऑपरेटरों का उपयोग करें। उदाहरण: संग्रह और दस्तावेज़ बनाना > db.demo391.insertOne({ _id : 101, Name : Chris

  1. - MongoDB Array में Element Remove करना

    Element remove करने के लिए update में $pull का उपयोग करें। $pull operator existing array से उन सभी instances को remove करता है जो specified condition से match करते हैं। Collection बनाना db.demo541.insertOne({software:{services:[gmail,facebook,yahoo]}}); { acknowledged : true, insertedId : ObjectId(