
- 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
Removing an array element from a MongoDB collection
To remove an array element, simply use $pull along with update(). Let us create a collection with documents −
> db.demo146.insertOne({"ListOfEmployeeNames":["Chris","David","Bob","Mike"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e32f54ffdf09dd6d08539bd") }
Display all documents from a collection with the help of find() method −
> db.demo146.find();
This will produce the following output −
{ "_id" : ObjectId("5e32f54ffdf09dd6d08539bd"), "ListOfEmployeeNames" : [ "Chris", "David", "Bob", "Mike" ] }
Following is the query to remove an array element from MongoDB −
> db.demo146.update({}, { "$pull": { "ListOfEmployeeNames": "Bob" } }); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo146.find();
This will produce the following output −
{ "_id" : ObjectId("5e32f54ffdf09dd6d08539bd"), "ListOfEmployeeNames" : [ "Chris", "David", "Mike" ] }
- Related Articles
- Removing an array element from MongoDB collection using update() and $pull
- MongoDB query to pull array element from a collection?
- Removing an element from an Array in Javascript
- Removing an object in a child collection in MongoDB?
- Removing item from array in MongoDB?
- Removing an element from a given position of the array in Javascript
- How to get array from a MongoDB collection?
- Retrieve only the queried element in an object array in MongoDB collection?
- Removing an element from the end of the array in Javascript
- Removing an element from the start of the array in javascript
- Unset an attribute from a single array element in MongoDB?
- How to delete element from an array in MongoDB?
- Removing the odd occurrence of any number/element from an array in JavaScript
- MongoDB query to remove entire array from collection?
- MongoDB query to match and remove element from an array?

Advertisements