
- 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 do I delete array value from a document in MongoDB?
To delete array values, use $pull in MongoDB. The $pull operator removes from an existing array all instances of a value or values that match a specified condition.
Let us first create a collection with documents −
> db.demo535.insertOne( ... { ... ... "studentId" : "101", ... "studentName" : "Chris", ... "ListOfMailIds" : [ ... "Chris@gmail.com", ... "Chris@yahoo.com" ... ] ... ... } ... ) { "acknowledged" : true, "insertedId" : ObjectId("5e8c82bfef4dcbee04fbbc00") }
Display all documents from a collection with the help of find() method −
> db.demo535.find();
This will produce the following output −
{ "_id" : ObjectId("5e8c82bfef4dcbee04fbbc00"), "studentId" : "101", "studentName" : "Chris", "ListOfMailIds" : [ "Chris@gmail.com", "Chris@yahoo.com" ] }
Following is the query to delete array value from a document in MongoDB −
> db.demo535.update( ... { _id: ObjectId("5e8c82bfef4dcbee04fbbc00") }, ... { $pull: { 'ListOfMailIds': 'Chris@yahoo.com' } } ... ); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo535.find();
This will produce the following output −
{ "_id" : ObjectId("5e8c82bfef4dcbee04fbbc00"), "studentId" : "101", "studentName" : "Chris", "ListOfMailIds" : [ "Chris@gmail.com" ] }
- Related Articles
- How do I remove a string from an array in a MongoDB document?
- How to delete a document in MongoDB?
- How to delete document from a collection in MongoDB using deleteOne() method?
- How do I get email-id from a MongoDB document and display with print()
- How to delete a MongoDB document using Java?
- How to delete particular data in a document in MongoDB?
- How do I get a value array (instead a json array) greater than 50 in MongoDB?
- How do I add a value to the top of an array in MongoDB?
- How to delete element from an array in MongoDB?
- How to delete document by _id using MongoDB?
- How to query a document in MongoDB comparing fields from an array?
- In MongoDB how do you use $set to update a nested value/embedded document?
- Find document with array that contains a specific value in MongoDB
- Find the MongoDB document from sub array?
- MongoDB query to remove array elements from a document?

Advertisements