
- 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 remove element in a MongoDB array?
To remove an element, update, and 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.demo541.insertOne({"software":{"services":["gmail","facebook","yahoo"]}});{ "acknowledged" : true, "insertedId" : ObjectId("5e8ca845ef4dcbee04fbbc11") } > db.demo541.insertOne({"software":{"services":["whatsapp","twitter"]}});{ "acknowledged" : true, "insertedId" : ObjectId("5e8ca85cef4dcbee04fbbc12") }
Display all documents from a collection with the help of find() method −
> db.demo541.find();
This will produce the following output −
{ "_id" : ObjectId("5e8ca845ef4dcbee04fbbc11"), "software" : { "services" : [ "gmail", "facebook", "yahoo" ] } } { "_id" : ObjectId("5e8ca85cef4dcbee04fbbc12"), "software" : { "services" : [ "whatsapp", "twitter" ] } }
Following is the query to remove an element in a MongoDB array −
> db.demo541.update({ _id: ObjectId("5e8ca845ef4dcbee04fbbc11") }, ... { $pull: { 'software.services': "yahoo" }} ... ); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo541.find();
This will produce the following output −
{ "_id" : ObjectId("5e8ca845ef4dcbee04fbbc11"), "software" : { "services" : [ "gmail", "facebook" ] } } { "_id" : ObjectId("5e8ca85cef4dcbee04fbbc12"), "software" : { "services" : [ "whatsapp", "twitter" ] } }
- Related Articles
- How to remove array element in MongoDB?
- How to remove a specific element from array in MongoDB?
- How to remove an array element by its index in MongoDB?
- Remove null element from MongoDB array?
- How to remove an element from a doubly-nested array in a MongoDB document?
- How do you remove an array element by its index in MongoDB
- MongoDB query to match and remove element from an array?
- MongoDB query to remove element from array as sub property
- How to remove object from array in MongoDB?
- How to remove Specific Element from a Swift Array?
- How to get a particular element from MongoDB array?
- How to remove a specific element from a JSON Array in Java?
- How to delete/remove an element from a C# array?
- How to delete element from an array in MongoDB?
- How to push an element into array in MongoDB?

Advertisements