
- 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 a field completely from a MongoDB document?
You can use $unset operator to remove a field completely from a MongoDb document. The syntax is as follows:
db.yourCollectionName.update({}, {$unset: {yourFieldName:1}}, false, true);
To understand the above syntax, let us create a collection with some documents. The query to create a collection with documents are as follows:
> db.removeFieldCompletlyDemo.insertOne({"StudentName":"Larry","StudentFavouriteSubject": ["Java","C","C++","Python"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c6ef55a6fd07954a48906a3") } > db.removeFieldCompletlyDemo.insertOne({"StudentName":"Mike","StudentFavouriteSubject": ["Javascript","HTML5","CSS3"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c6ef57b6fd07954a48906a4") } > db.removeFieldCompletlyDemo.insertOne({"StudentName":"Sam","StudentFavouriteSubject": ["MongoDB","MySQL","SQL Server"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c6ef59c6fd07954a48906a5") }
Display all documents from a collection with the help of find() method. The query is as follows:
> db.removeFieldCompletlyDemo.find().pretty();
The following is the output:
{ "_id" : ObjectId("5c6ef55a6fd07954a48906a3"), "StudentName" : "Larry", "StudentFavouriteSubject" : [ "Java", "C", "C++", "Python" ] } { "_id" : ObjectId("5c6ef57b6fd07954a48906a4"), "StudentName" : "Mike", "StudentFavouriteSubject" : [ "Javascript", "HTML5", "CSS3" ] } { "_id" : ObjectId("5c6ef59c6fd07954a48906a5"), "StudentName" : "Sam", "StudentFavouriteSubject" : [ "MongoDB", "MySQL", "SQL Server" ] }
Remove field "StudentFavouriteSubject" from documents. The query is as follows:
> db.removeFieldCompletlyDemo.update({}, {$unset: {StudentFavouriteSubject:1}}, false, true); WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
We have removed the field "StudentFavouriteSubject" from documents. Let us now display all documents from a collection to verify.
The query is as follows:
> db.removeFieldCompletlyDemo.find().pretty();
The following is the output:
{ "_id" : ObjectId("5c6ef55a6fd07954a48906a3"), "StudentName" : "Larry" } { "_id" : ObjectId("5c6ef57b6fd07954a48906a4"), "StudentName" : "Mike" } { "_id" : ObjectId("5c6ef59c6fd07954a48906a5"), "StudentName" : "Sam" }
- Related Articles
- Remove all except a single field from a nested document via projection in MongoDB
- MongoDB query to remove array elements from a document?
- MongoDB query to remove subdocument from document?
- Remove values from a matrix like document in MongoDB
- How to remove an element from a doubly-nested array in a MongoDB document?
- How to remove all documents from a collection except a single document in MongoDB?
- MongoDB query to remove a specific document
- How do I remove a string from an array in a MongoDB document?
- How to sum every field in a sub document of MongoDB?
- Remove only a single document in MongoDB
- How to subtract values (TotalPrice – Discount) from document field values in MongoDB?
- MongoDB query select and display only a specific field from the document?
- How to add an extra field in a sub document in MongoDB?
- How to select MongoDB document that does not consist a specific field?
- Add new field to every document in a MongoDB collection?

Advertisements