- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MongoDB query to remove element from array as sub property
To remove, use $pull in MongoDB. Let us first create a collection with documents −
> db.demo388.insertOne( ... { ... _id: '101', ... userDetails: { ... isMarried: false, ... userInfo: [ ... { ... Name:"Chris", ... Age:21 ... ... } ... ] ... } ... } ... ); { "acknowledged" : true, "insertedId" : "101" }
Display all documents from a collection with the help of find() method −
> db.demo388.find();
This will produce the following output −
{ "_id" : "101", "userDetails" : { "isMarried" : false, "userInfo" : [ { "Name" : "Chris", "Age" : 21 } ] } }
Following is the query to remove element from array as sub property −
> db.demo388.update( ... { "_id": "101" }, ... { "$pull": { "userDetails.userInfo": { "Name":"Chris" } } ... }) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo388.find();
This will produce the following output −
{ "_id" : "101", "userDetails" : { "isMarried" : false, "userInfo" : [ ] } }
- Related Articles
- MongoDB query to match and remove element from an array?
- MongoDB query to remove item from array?
- MongoDB query to remove entire array from collection?
- Remove null element from MongoDB array?
- MongoDB query to remove array elements from a document?
- MongoDB query to find property of first element of array?
- MongoDB query to pull array element from a collection?
- How to remove a specific element from array in MongoDB?
- Pull an element in sub of sub-array in MongoDB?
- MongoDB query to set a sub item in an array?
- MongoDB query to remove subdocument from document?
- How to remove array element in MongoDB?
- MongoDB query for capped sub-collection in an array
- MongoDB query to get record beginning with specific element from an array?
- How to remove element in a MongoDB array?

Advertisements