MongoDB में $unset ऑपरेटर से फील्ड्स (कीज) रिमूव करना
MongoDB में डॉक्यूमेंट्स से विशिष्ट फील्ड्स (कीज) को पूरी तरह हटाने के लिए $unset ऑपरेटर का उपयोग करें।
उदाहरण: कलेक्शन और डॉक्यूमेंट्स बनाना
> db.removeKeyFieldsDemo.insertOne({"StudentFirstName":"John","StudentLastName":"Doe","StudentAge":23});
{ "acknowledged" : true, "insertedId" : ObjectId("5cc6c8289cb58ca2b005e672") }
> db.removeKeyFieldsDemo.insertOne({"StudentFirstName":"John","StudentLastName":"Smith","StudentAge":21});
{ "acknowledged" : true, "insertedId" : ObjectId("5cc6c8359cb58ca2b005e673") }
सभी डॉक्यूमेंट्स देखना
> db.removeKeyFieldsDemo.find().pretty();
आउटपुट:
{ "_id" : ObjectId("5cc6c8289cb58ca2b005e672"), "StudentFirstName" : "John", "StudentLastName" : "Doe", "StudentAge" : 23 }
{ "_id" : ObjectId("5cc6c8359cb58ca2b005e673"), "StudentFirstName" : "John", "StudentLastName" : "Smith", "StudentAge" : 21 }
सभी डॉक्यूमेंट्स से StudentAge फील्ड रिमूव करना
> db.removeKeyFieldsDemo.updateMany({}, {$unset: {StudentAge: 1}});
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }
अपडेटेड डॉक्यूमेंट्स देखना
> db.removeKeyFieldsDemo.find().pretty();
आउटपुट:
{ "_id" : ObjectId("5cc6c8289cb58ca2b005e672"), "StudentFirstName" : "John", "StudentLastName" : "Doe" }
{ "_id" : ObjectId("5cc6c8359cb58ca2b005e673"), "StudentFirstName" : "John", "StudentLastName" : "Smith" }
मुख्य बिंदु:
$unset: { fieldName: 1 }- फील्ड को पूरी तरह डॉक्यूमेंट से हटा देता है- वैल्यू
1केवल प्लेसहोल्डर है, कोई भी ट्रुथी वैल्यू काम करती है updateMany()सभी मैचिंग डॉक्यूमेंट्स पर लागू करता है- एक साथ कई फील्ड्स हटाने के लिए:
{$unset: {field1: 1, field2: 1, field3: 1}} $set: {field: null}के विपरीत,$unsetफील्ड को पूरी तरह हटा देता है