- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Removing item from array in MongoDB?
To remove item from array, use $pull in MongoDB. Let us create a collection with documents −
> db.demo224.insertOne({"ListOfTechnology":["Spring","Hibernate","Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ee6d103d395bdc2134733") } > db.demo224.insertOne({"ListOfTechnology":["Groovy"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ee6ec03d395bdc2134734") }
Display all documents from a collection with the help of find() method −
> db.demo224.find();
This will produce the following output −
{ "_id" : ObjectId("5e3ee6d103d395bdc2134733"), "ListOfTechnology" : [ "Spring", "Hibernate", "Java" ] } { "_id" : ObjectId("5e3ee6ec03d395bdc2134734"), "ListOfTechnology" : [ "Groovy" ] }
Following is the query to remove item from array in MongoDB −
>db.demo224.update({_id:ObjectId("5e3ee6d103d395bdc2134733")},{$pull:{"ListOfTechnology":"Java"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo224.find();
This will produce the following output −
{ "_id" : ObjectId("5e3ee6d103d395bdc2134733"), "ListOfTechnology" : [ "Spring", "Hibernate" ] } { "_id" : ObjectId("5e3ee6ec03d395bdc2134734"), "ListOfTechnology" : [ "Groovy" ] }
Advertisements