
- 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
MongoDB query to remove subdocument from document?
To remove subdocument from a document, use $pull along with update(). Let us first create a collection with documents −
> db.demo538.insertOne( ... { ... id:101, ... "details": ... { ... anotherDetails: ... [ ... { ... "Name":"Chris", ... Age:21 ... }, ... { ... "Name":"David", ... Age:23 ... }, ... { ... "Name":"Bob", ... Age:20 ... } ... ] ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e8c8f0aef4dcbee04fbbc08") }
Display all documents from a collection with the help of find() method −
> db.demo538.find();
This will produce the following output −
{ "_id" : ObjectId("5e8c8f0aef4dcbee04fbbc08"), "id" : 101, "details" : { "anotherDetails" : [ { "Name" : "Chris", "Age" : 21 }, { "Name" : "David", "Age" : 23 }, { "Name" : "Bob", "Age" : 20 } ] } }
Following is the query to remove subdocument from a document −
> db.demo538.update({ id:101}, ... {$pull : { "details.anotherDetails" : {"Age":23} } } ) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo538.find();
This will produce the following output −
{ "_id" : ObjectId("5e8c8f0aef4dcbee04fbbc08"), "id" : 101, "details" : { "anotherDetails" : [ { "Name" : "Chris", "Age" : 21 }, { "Name" : "Bob", "Age" : 20 } ] } }
- Related Articles
- MongoDB query to remove array elements from a document?
- MongoDB query to remove a specific document
- Query MongoDB subdocument to print on one line?
- Display MongoDB with document and subdocument example and update
- MongoDB query to remove item from array?
- MongoDB query to find and return subdocument with criteria?
- How to sort, select and query subdocument in MongoDB?
- How to remove a field completely from a MongoDB document?
- MongoDB query to remove entire array from collection?
- MongoDB query to update a specific document from a collection
- MongoDB query to pull a specific value from a document
- MongoDB query to update nested document
- Remove values from a matrix like document in MongoDB
- MongoDB query to remove entire data from a collection
- MongoDB query to return only embedded document?

Advertisements