 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP 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
Pull an element in sub of sub-array in MongoDB?
To pull an element, use $pull along with $(positional) operator. Let us create a collection with documents −
> db.demo679.insertOne( ...    { ...       id:1, ...       "details": [ ...          { ...             CountryName:"US", ...             "information": [ ... ...                { "Name": "Chris", "FirstName": "Name=Chris" }, ... ...                {"Name": "Bob", "FirstName": "Name=Bob" } ...             ] ...          }, ...          { ...             CountryName:"UK", ...             "information": [ ... ...                { "Name": "Robert", "FirstName": "Name=Robert" }, ... ...                {"Name": "Sam", "FirstName": "Name=Sam" } ...             ] ...          } ...       ] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea442cf04263e90dac943fd") } Display all documents from a collection with the help of find() method −
> db.demo679.find();
This will produce the following output −
{ "_id" : ObjectId("5ea442cf04263e90dac943fd"), "id" : 1, "details" : [    { "CountryName" : "US", "information" :  [       { "Name" : "Chris", "FirstName" : "Name=Chris" },       { "Name" : "Bob", "FirstName" : "Name=Bob" }    ] },    { "CountryName" : "UK", "information" : [       { "Name" : "Robert", "FirstName" : "Name=Robert" },       { "Name" : "Sam", "FirstName" : "Name=Sam" }    ] } ] } Following is the query to pull an element in sub of sub-array in MongoDB −
> db.demo679.update( ...    { "details.CountryName":"US" }, ...    { $pull: { 'details.$.information': { "Name" : "Bob", "FirstName" : "Name=Bob" } } } ... ); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) Display all documents from a collection with the help of find() method −
> db.demo679.find().pretty();
This will produce the following output −
{    "_id" : ObjectId("5ea442cf04263e90dac943fd"),    "id" : 1,    "details" : [       {          "CountryName" : "US",          "information" : [             {                "Name" : "Chris",                "FirstName" : "Name=Chris"             }          ]       },       {          "CountryName" : "UK",          "information" : [             {                "Name" : "Robert",                "FirstName" : "Name=Robert"             },             {                "Name" : "Sam",                "FirstName" : "Name=Sam"             }          ]       }    ] }Advertisements
 