- 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
Delete a field and value in MongoDB?
To delete a MongoDB field and value, you can use $unset operator. Let us first create a collection with documents −
> db.deleteFieldDemo.insertOne({"FirstName":"John","LastName":"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5cb9fb767219729fde21ddad") } > db.deleteFieldDemo.insertOne({"FirstName":"David","LastName":"Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5cb9fb837219729fde21ddae") } > db.deleteFieldDemo.insertOne({"FirstName":"Carol","LastName":"Taylor"}); { "acknowledged" : true, "insertedId" : ObjectId("5cb9fb8d7219729fde21ddaf") }
Following is the query to display all documents from the collection with the help of find() method −
> db.deleteFieldDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cb9fb767219729fde21ddad"), "FirstName" : "John", "LastName" : "Smith" } { "_id" : ObjectId("5cb9fb837219729fde21ddae"), "FirstName" : "David", "LastName" : "Miller" } { "_id" : ObjectId("5cb9fb8d7219729fde21ddaf"), "FirstName" : "Carol", "LastName" : "Taylor" }
Following is the query to delete a field with value −
> db.deleteFieldDemo.update( ... { FirstName: { $exists: true } }, ... { $unset: { FirstName: 1 } }, ... false, ... true ... ); WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
Let us check the field FirstName have been deleted from the collection or not −
> db.deleteFieldDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cb9fb767219729fde21ddad"), "LastName" : "Smith" } { "_id" : ObjectId("5cb9fb837219729fde21ddae"), "LastName" : "Miller" } { "_id" : ObjectId("5cb9fb8d7219729fde21ddaf"), "LastName" : "Taylor" }
- Related Articles
- How to search for a record (field) and then delete it in MongoDB?
- Delete all elements in an array field in MongoDB?
- Update MongoDB field using value of another field?
- Sorting field value (FirstName) for MongoDB?
- How do I delete array value from a document in MongoDB?
- How to return only value of a field in MongoDB?
- Check if value exists for a field in a MongoDB document?
- Set MongoDB compound index with a fixed value field
- Replace an array field value with MongoDB?
- How to exclude array type field value in MongoDB?
- MongoDB query (aggregation framework) to match a specific field value
- How do I check whether a field contains null value in MongoDB?
- Find the document by field name with a specific value in MongoDB?
- Match MongoDB documents with field value greater than a specific number and fetch them?
- Is it possible to use MongoDB field value as a pattern in $regex?

Advertisements