
- 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
How to remove object from array in MongoDB?
You can use $pull operator to remove the object from an array in MongoDB. 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.removeObjectFromArrayDemo.insertOne( ... { ... ... "StudentName": "John", ... "StudentAcademicProjectDetails": ... [{ ... "StudentProjectId": 101, ... "StudentProjectName": "Pig Dice Game" ... }, ... { ... "StudentProjectId": 110, ... "StudentProjectName": "Library Management System" ... }, ... ... { ... "StudentProjectId": 120, ... "StudentProjectName": "Phonebook Management System" ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5c8ad13d6cea1f28b7aa0817") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.removeObjectFromArrayDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c8ad13d6cea1f28b7aa0817"), "StudentName" : "John", "StudentAcademicProjectDetails" : [ { "StudentProjectId" : 101, "StudentProjectName" : "Pig Dice Game" }, { "StudentProjectId" : 110, "StudentProjectName" : "Library Management System" }, { "StudentProjectId" : 120, "StudentProjectName" : "Phonebook Management System" } ] }
Here is the query to remove the object from an array in MongoDB −
> db.removeObjectFromArrayDemo.update( ... {'_id': ObjectId("5c8ad13d6cea1f28b7aa0817")}, ... { $pull: { "StudentAcademicProjectDetails" : { StudentProjectId: 101 } } }, ... false, ... true ... ); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Let us check the documents from collection to check the object has been removed from an array or not. The query is as follows −
> db.removeObjectFromArrayDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c8ad13d6cea1f28b7aa0817"), "StudentName" : "John", "StudentAcademicProjectDetails" : [ { "StudentProjectId" : 110, "StudentProjectName" : "Library Management System" }, { "StudentProjectId" : 120, "StudentProjectName" : "Phonebook Management System" } ] }
Look at the above sample output, the field “StudentProjectId” with 101 has been removed.
- Related Articles
- Remove object from array in MongoDB?
- How to remove a specific element from array in MongoDB?
- MongoDB query to remove empty objects in an object-array?
- MongoDB query to remove item from array?
- How to get items from an object array in MongoDB?
- How to remove array element in MongoDB?
- PHP: Remove object from array
- Remove null element from MongoDB array?
- MongoDB query to remove entire array from collection?
- How to remove element in a MongoDB array?
- MongoDB query to remove array elements from a document?
- Getting distinct values from object array in MongoDB?
- How to remove an element from a doubly-nested array in a MongoDB document?
- MongoDB query to match and remove element from an array?
- MongoDB query to remove element from array as sub property

Advertisements