
- 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
How to delete element from an array in MongoDB?
To delete element from an array, use $pull. Let us create a collection with documents −
> db.demo279.insertOne({id:[101,103,105,110]}); { "acknowledged" : true, "insertedId" : ObjectId("5e490af7dd099650a5401a58") } > db.demo279.insertOne({id:[107,111,110]}); { "acknowledged" : true, "insertedId" : ObjectId("5e490b06dd099650a5401a59") }
Display all documents from a collection with the help of find() method −
> db.demo279.find();
This will produce the following output −
{ "_id" : ObjectId("5e490af7dd099650a5401a58"), "id" : [ 101, 103, 105, 110 ] } { "_id" : ObjectId("5e490b06dd099650a5401a59"), "id" : [ 107, 111, 110 ] }
Following is the query to delete element from an array &minus';
> db.demo279.update({},{$pull:{id:110}},{multi:true}); WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
Display all documents from a collection with the help of find() method −
> db.demo279.find();
This will produce the following output −
{ "_id" : ObjectId("5e490af7dd099650a5401a58"), "id" : [ 101, 103, 105 ] } { "_id" : ObjectId("5e490b06dd099650a5401a59"), "id" : [ 107, 111 ] }
- Related Articles
- How to delete/remove an element from a C# array?
- How to delete n-th element of array in MongoDB?
- How to delete an element from an array in PHP and re-index the array?
- Delete specific record from an array nested within another array in MongoDB?
- How to delete elements from an array?
- How to push an element into array in MongoDB?
- Removing an array element from a MongoDB collection
- How to delete an array element based on key in PHP?
- How to use MongoDB $pull to delete documents within an Array?
- MongoDB query to match and remove element from an array?
- How to remove an element from a doubly-nested array in a MongoDB document?
- How can I delete an item from an Object in MongoDB?
- How to remove a specific element from array in MongoDB?
- Unset an attribute from a single array element in MongoDB?
- How to get a particular element from MongoDB array?

Advertisements