

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- How to delete/remove an element from a C# array?
- How to delete an element from an array in PHP and re-index the array?
- How to delete n-th element of array in MongoDB?
- How to delete elements from an array?
- Delete specific record from an array nested within another array in MongoDB?
- Removing an array element from a MongoDB collection
- How to delete a particular element from a JavaScript array?
- MongoDB query to match and remove element from an array?
- How can I delete an item from an Object in MongoDB?
- How to push an element into array in MongoDB?
- How to delete an array element based on key in PHP?
- How to use MongoDB $pull to delete documents within an Array?
- Unset an attribute from a single array element in MongoDB?
- How to get a particular element from MongoDB array?
- PHP program to delete an element from the array using the unset function
Advertisements