
- 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
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" } ] } ] }
- Related Articles
- How do you limit an array sub-element in MongoDB?
- MongoDB query for capped sub-collection in an array
- How to add a sub-document to sub-document array in MongoDB?
- MongoDB query to set a sub item in an array?
- MongoDB query to remove element from array as sub property
- Filter sub documents by sub document in MongoDB?
- Find the MongoDB document from sub array?
- Updating sub-object in MongoDB?
- Finding an element in a sub-element in Selenium Webdriver.
- Working with MongoDB $sort for sub array document
- How to pull an array element (which is a document) in MongoDB?
- Removing an array element from MongoDB collection using update() and $pull
- MongoDB query by sub-field?
- Sum MongoDB Sub-documents field?
- MongoDB filter multiple sub-documents?

Advertisements