Array values delete करने के लिए MongoDB में $pull का उपयोग करें। $pull operator existing array से उन सभी instances को remove करता है जो specified condition से match करते हैं।
Collection बनाना
> db.demo535.insertOne(
... {
... "studentId" : "101",
... "studentName" : "Chris",
... "ListOfMailIds" : [
... "Chris@gmail.com",
... "Chris@yahoo.com"
... ]
... }
... )
{ "acknowledged" : true, "insertedId" : ObjectId("5e8c82bfef4dcbee04fbbc00") }
सभी Documents Display करना
> db.demo535.find();
Output:
{ "_id" : ObjectId("5e8c82bfef4dcbee04fbbc00"), "studentId" : "101", "studentName" : "Chris", "ListOfMailIds" : [ "Chris@gmail.com", "Chris@yahoo.com" ] }
Array Value Delete करने का Query
> db.demo535.update(
... { _id: ObjectId("5e8c82bfef4dcbee04fbbc00") },
... { $pull: { 'ListOfMailIds': 'Chris@yahoo.com' } }
... );
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Updated Documents Display करना
> db.demo535.find();
Output:
{ "_id" : ObjectId("5e8c82bfef4dcbee04fbbc00"), "studentId" : "101", "studentName" : "Chris", "ListOfMailIds" : [ "Chris@gmail.com" ] }
मुख्य बिंदु: $pull specific value या condition के आधार पर array elements remove करता है। यदि same value multiple times हो तो सभी instances remove हो जाएंगे।