

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to delete particular data in a document in MongoDB?
You can use $unset. Let us first create a collection with documents −
> db.demo20.insertOne( ... { ... ... "ListOfEmployee" : [ ... { ... "EmployeeName1" : "John" ... }, ... { ... "EmployeeName2" : "Carol" ... } ... ], ... "EmployeeName2" : [] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e138c3555d0fc6657d21f12") }
Following is the query to display all documents from a collection with the help of find() method −
> db.demo20.find();
This will produce the following output −
{ "_id" : ObjectId("5e138c3555d0fc6657d21f12"), "ListOfEmployee" : [ { "EmployeeName1" : "John" }, { "EmployeeName2" : "Carol" } ], "EmployeeName2" : [ ] }
Following is the query to delete particular data in a document −
> db.demo20.update({ "EmployeeName2": { "$exists": 1 }},{ "$unset": { "EmployeeName2": "" } }); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Following is the query to display all documents from a collection with the help of find() method −
> db.demo20.find();
This will produce the following output −
{ "_id" : ObjectId("5e138c3555d0fc6657d21f12"), "ListOfEmployee" : [ { "EmployeeName1" : "John" }, { "EmployeeName2" : "Carol" } ] }
- Related Questions & Answers
- How to delete a document in MongoDB?
- How to delete a MongoDB document using Java?
- How to reach subdata in MongoDB and display a particular document?
- How to delete document by _id using MongoDB?
- How to get embedded data in a MongoDB document?
- How to delete partial data in MongoDB?
- How to delete document from a collection in MongoDB using deleteOne() method?
- How do I delete array value from a document in MongoDB?
- Delete partial data in MongoDB?
- How to get a particular anchor in a document in JavaScript?
- MongoDB query to fetch a document that does not have a particular field?
- How to add a sub-document to sub-document array in MongoDB?
- How to delete everything in a MongoDB database?
- How to delete a particular element from a JavaScript array?
- Delete data from a collection in MongoDB using multiple conditions?
Advertisements