- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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" ] }
- Related Articles
- MongoDB query to remove item from array?
- Removing an array element from a MongoDB collection
- Removing empty fields from MongoDB
- Removing an array element from MongoDB collection using update() and $pull
- MongoDB query to gather unique array item?
- Removing Negatives from Array in JavaScript
- Removing duplicate objects from array in JavaScript
- How to add new item in nested array with MongoDB?
- MongoDB query to set a sub item in an array?
- Removing an element from an Array in Javascript
- Removing duplicate elements from an array in PHP
- Removing comments from array of string in JavaScript
- Find document in MongoDB where at least one item from an array is not in the other?
- Search for documents matching first item in an array with MongoDB?
- Getting only the first item for an array property in MongoDB?

Advertisements