
- 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
Removing an object in a child collection in MongoDB?
To remove an object in a child collection, use $pull in MongoDB. Let us create a collection with documents −
> db.demo715.insertOne({ ... _id:1, ... details : ... [ ... { 'id' : '101', ... 'Information' : [ ... { ... 'studentid' : '102', ... "Name":"Bob" ... }, ... { ... 'studentid' : '103', ... "Name":"Chris" ... } ... ] ... } ... ] ... }); { "acknowledged" : true, "insertedId" : 1 }
Display all documents from a collection with the help of find() method −
> db.demo715.find();
This will produce the following output −
{ "_id" : 1, "details" : [ { "id" : "101", "Information" : [ { "studentid" : "102", "Name" : "Bob" }, { "studentid" : "103", "Name" : "Chris" } ] } ] }
Following is the query to remove an object in a child collection in MongoDB −
> db.demo715.update( ... {"details.id":'101'}, ... { $pull: { 'details.$.Information':{studentid:'102',"Name":"Bob"} } } ... ); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo715.find();
This will produce the following output −
{ "_id" : 1, "details" : [ { "id" : "101", "Information" : [ { "studentid" : "103", "Name" : "Chris" } ] } ] }
- Related Articles
- Removing an array element from a MongoDB collection
- Removing an array element from MongoDB collection using update() and $pull
- Removing first occurrence of object from Collection in C#
- Retrieve only the queried element in an object array in MongoDB collection?
- MongoDB query to find last object in collection?
- How to get the child of a MongoDB collection by the key?
- Clone a collection in MongoDB?
- Create index in a MongoDB collection?
- MongoDB query to add a document in an already created collection
- MongoDB query for capped sub-collection in an array
- Removing item from array in MongoDB?
- Renaming column name in a MongoDB collection?
- How to drop a collection in MongoDB?
- Group by dates in a MongoDB collection?
- Removing property from a JSON object in JavaScript

Advertisements