To pull a specific value, use UPDATE with $pull. Let us create a collection with documents −
> db.demo318.insertOne({Subject:["MySQL","MongoDB","Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e50ea6df8647eb59e562062") } > db.demo318.insertOne({Subject:["Spring","Hibernate"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e50ea78f8647eb59e562063") }
Display all documents from a collection with the help of find() method −
> db.demo318.find();
This will produce the following output −
{ "_id" : ObjectId("5e50ea6df8647eb59e562062"), "Subject" : [ "MySQL", "MongoDB", "Java" ] } { "_id" : ObjectId("5e50ea78f8647eb59e562063"), "Subject" : [ "Spring", "Hibernate" ] }
Following is the query to $pull a specific value “Hibernate” −
> db.demo318.update({Subject:"Hibernate"},{$pull:{"Subject":"Hibernate"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo318.find();
This will produce the following output −
{ "_id" : ObjectId("5e50ea6df8647eb59e562062"), "Subject" : [ "MySQL", "MongoDB", "Java" ] } { "_id" : ObjectId("5e50ea78f8647eb59e562063"), "Subject" : [ "Spring" ] }