
- 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 an element from a doubly-nested array in a MongoDB document?
To remove an element from a doubly-nested array in MongoDB document, you can use $pull operator.
To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.removeElementFromDoublyNestedArrayDemo.insertOne( ... { ... "_id" : "1", ... "UserName" : "Larry", ... "UserDetails" : [ ... { ... "UserCountryName" : "US", ... "UserLocation" : [ ... { ... "UserCityName" : "New York" ... }, ... { ... "UserZipCode" : "10001" ... } ... ] ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : "1" } > db.removeElementFromDoublyNestedArrayDemo.insertOne( ... { ... "_id" : "2", ... "UserName" : "Mike", ... "UserDetails" : [ ... { ... "UserCountryName" : "UK", ... "UserLocation" : [ ... { ... "UserCityName" : "Bangor" ... }, ... { ... "UserZipCode" : "20010" ... } ... ] ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : "2" }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.removeElementFromDoublyNestedArrayDemo.find().pretty();
The following is the output −
{ "_id" : "1", "UserName" : "Larry", "UserDetails" : [ { "UserCountryName" : "US", "UserLocation" : [ { "UserCityName" : "New York" }, { "UserZipCode" : "10001" } ] } ] } { "_id" : "2", "UserName" : "Mike", "UserDetails" : [ { "UserCountryName" : "UK", "UserLocation" : [ { "UserCityName" : "Bangor" }, { "UserZipCode" : "20010" } ] } ] }
Here is the query to remove an element from a doubly-nested array in MongoDB document −
> db.removeElementFromDoublyNestedArrayDemo.update( ... { _id : "2" }, ... {$pull : {"UserDetails.0.UserLocation" : {"UserZipCode":"20010"}}} ... ); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Lets us check the documents from a collection with the help of find(). The query is as follows −
> db.removeElementFromDoublyNestedArrayDemo.find().pretty();
The following is the output −
{ "_id" : "1", "UserName" : "Larry", "UserDetails" : [ { "UserCountryName" : "US", "UserLocation" : [ { "UserCityName" : "New York" }, { "UserZipCode" : "10001" } ] } ] } { "_id" : "2", "UserName" : "Mike", "UserDetails" : [ { "UserCountryName" : "UK", "UserLocation" : [ { "UserCityName" : "Bangor" } ] } ] }
Now field "UserZipCode": "20010" has been removed from a doubly-nested array.
- Related Articles
- How do I remove a string from an array in a MongoDB document?
- MongoDB query to remove array elements from a document?
- How to remove a specific element from array in MongoDB?
- Remove all except a single field from a nested document via projection in MongoDB
- Extract a particular element from a nested array in MongoDB
- How to pull an array element (which is a document) in MongoDB?
- How to remove element in a MongoDB array?
- Aggregate a $slice to get an element in exact position from a nested array in MongoDB?
- How to remove a field completely from a MongoDB document?
- How to query a document in MongoDB comparing fields from an array?
- MongoDB query to match and remove element from an array?
- How to project specific fields from a document inside an array in Mongodb?
- How to delete/remove an element from a C# array?
- Remove null element from MongoDB array?
- How to remove array element in MongoDB?

Advertisements