
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
MongoDB query to remove item from array?
To remove item from array, you can use $pull operator. Let us create a collection with documents −
> db.removeItemFromArray.insertOne( { "_id":101, "StudentName":"Larry", "StudentSubjects":["C","MongoDB","Java","MySQL"] } ); { "acknowledged" : true, "insertedId" : 101 }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.removeItemFromArray.find().pretty();
This will produce the following output −
{ "_id" : 101, "StudentName" : "Larry", "StudentSubjects" : [ "C", "MongoDB", "Java", "MySQL" ] }
Following is the query to remove item from an array −
> db.removeItemFromArray.update( ... { }, ... { $pull: {StudentSubjects:"Java" } }, ... { multi: true } ... ); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
In the above query, we have removed “Java”. Let us now display the documents from the collection −
> db.removeItemFromArray.find().pretty();
This will produce the following output −
{ "_id" : 101, "StudentName" : "Larry", "StudentSubjects" : [ "C", "MongoDB", "MySQL" ] }
- Related Articles
- MongoDB query to remove entire array from collection?
- MongoDB query to gather unique array item?
- MongoDB query to remove array elements from a document?
- MongoDB query to match and remove element from an array?
- MongoDB query to remove element from array as sub property
- MongoDB query to set a sub item in an array?
- MongoDB query to remove subdocument from document?
- Removing item from array in MongoDB?
- JavaScript Remove random item from array and then remove it from array until array is empty
- MongoDB query to remove entire data from a collection
- How to remove an item from JavaScript array by value?
- MongoDB query to remove empty objects in an object-array?
- Can we remove _id from MongoDB query result?
- Remove null element from MongoDB array?
- Remove object from array in MongoDB?

Advertisements