

- 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 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 Questions & Answers
- How do I remove a string from an array in a MongoDB document?
- MongoDB query to remove array elements from a document?
- Extract a particular element from a nested array in MongoDB
- How to remove a specific element from array in MongoDB?
- Remove all except a single field from a nested document via projection in MongoDB
- How to remove a field completely from a MongoDB document?
- 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 delete/remove an element from a C# array?
- How to query a document in MongoDB comparing fields from an array?
- Remove null element from MongoDB array?
- MongoDB query to match and remove element from an array?
- Remove values from a matrix like document in MongoDB
- How to project specific fields from a document inside an array in Mongodb?
Advertisements