MongoDB – How to remove a field in Mongo Document
Today in this article, we shall learn MongoDB – How to remove a field in MongoDB using Mongo shell CLI or UI query.
We will cover various 3-4 approaches to perform the remove fields feature. Today overall, We will cover the below aspects,
- Approach 1 – $unset Remove the field for all the documents in the database
- Approach 2 – Remove the field for all documents in the MongoDB
- Approach 3 – Remove the field for all documents in the database using updateMany
- Approach 4 – Remove the first field in documents in the database using updateOne
- Approach 5 – Remove the first field based on matching filter criteria using updateOne
- Delete records using the Delete or Remove method
The below approach discussed is very useful when you don’t want to delete or drop the Mongo collection but want to clear all existing documents based on certain matching criteria.
I have below Mongo Collection where my fields are defined as below.
I have a field name called “EmpID” which I want to delete this field using MongoShell
Let’s get started step by step with details on how to achieve rename.
Please open the command prompt or PowerShell and log in to your database using a connection string.
We will first log to MongoDB and then switch to using the required database and the collection
use <DBName>
Command Pattern
{ $unset: { <fieldName>:1} }
You can specify one or multiple fields for the renaming support.
Approach 1 – $unset Remove the field for all the documents in the database
Command:
db.employee.update( {}, { $unset: {'EmpID':1}},false,true )
Example
db.employee.update( {}, { $unset: {'EmpID':1}},false,true )
WriteResult({ "nMatched" : 4, "nUpserted" : 0, "nModified" : 4 })
Approach 2 – Remove the field for all documents in the MongoDB
Command:
db.employee.update( {}, { $unset: {'EmpID':1}},false,true )
Example
db.employee.update( {}, { $unset: {‘EmpID’:1}},false,true )
WriteResult({ “nMatched” : 4, “nUpserted” : 0, “nModified” : 4 })
Approach 3 – Remove the field for all documents in the database using updateMany
Using updateMany to Remove the field for all documents.
Command:
db.employee.updateMany({},{"$unset":{"EmpID":1}})
Example
db.employee.updateMany({},{"$unset":{"empID":1}})
{ "acknowledged" : true, "matchedCount" : 4, "modifiedCount" : 4 }
Approach 4 – Remove the first field in documents in the database using updateOne
We can use updateOne with $unset query
Command:
ddb.employee.updateOne( {}, { $unset: { EmpID:"" }})
Example
db.employee.updateOne( {}, { $unset: { EmpID:”” }})
{ “acknowledged” : true, “matchedCount” : 1, “modifiedCount” : 1 }
Approach 5 – Remove the first field based on matching filter criteria using updateOne
Below query removes the specified field if matches with a field for field name as “John”.
Command:
db.employee.updateOne( {Name:"John"}, { $unset: { EmpID:"" }})
Example
db.employee.updateOne( {}, { $unset: { EmpID:”” }})
{ “acknowledged” : true, “matchedCount” : 1, “modifiedCount” : 1 }
If you are using an embedded document or in an array, use dot notation to specify the field name.
Delete records using the Delete or Remove method
Remove or Delete is yet another simple technique One can delete single or multiple records using delete() or remove() method too.
For more details please visit below,
References:
Do you have any comments or ideas or any better suggestions to share?
Please sound off your comments below.
Happy Coding !!
Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.