स्थितीय $ के साथ $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उस फील्ड को पूरी तरह से हटा देता है।