
- 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 key fields in MongoDB?
To remove key fields in MongoFB, you can use $unset operator. Let us first create a collection with documents −
>db.removeKeyFieldsDemo.insertOne({"StudentFirstName":"John","StudentLastName":"Doe","StudentAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5cc6c8289cb58ca2b005e672") } >db.removeKeyFieldsDemo.insertOne({"StudentFirstName":"John","StudentLastName":"Smith","StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5cc6c8359cb58ca2b005e673") }
Following is the query to display all documents from a collection with the help of find() method −
> db.removeKeyFieldsDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cc6c8289cb58ca2b005e672"), "StudentFirstName" : "John", "StudentLastName" : "Doe", "StudentAge" : 23 } { "_id" : ObjectId("5cc6c8359cb58ca2b005e673"), "StudentFirstName" : "John", "StudentLastName" : "Smith", "StudentAge" : 21 }
Following is the query to remove key fields. Here, we are removing the StudentAge −
> db.removeKeyFieldsDemo.updateMany({},{$unset:{StudentAge:1}}); { "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }
Let us display all documents from the above collection −
> db.removeKeyFieldsDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cc6c8289cb58ca2b005e672"), "StudentFirstName" : "John", "StudentLastName" : "Doe" } { "_id" : ObjectId("5cc6c8359cb58ca2b005e673"), "StudentFirstName" : "John", "StudentLastName" : "Smith" }
- Related Articles
- How to remove primary key from MongoDB?
- Concatenate fields in MongoDB?
- How to find datatype of all the fields in MongoDB?
- How to compare two fields in aggregation filter with MongoDB?
- How to remove array element in MongoDB?
- How to get distinct values for non-key column fields in Laravel?
- Include all existing fields and add new fields to document in MongoDB?
- How to retrieve all nested fields from MongoDB collection?
- How to update record in MongoDB without replacing the existing fields?
- MongoDB query to sum specific fields
- MongoDB query to update selected fields
- How to remove object from array in MongoDB?
- How to remove element in a MongoDB array?
- MongoDB - how can I access fields in a document?
- Update only specific fields in MongoDB?

Advertisements