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.

Updated on: 30-Jul-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements