Delete field in MongoDB

Today in this article, we shall learn MongoDB – How to Remove fields from the document 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,

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

MongoDB How to RemoveDelete fields from the document Mongoose

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 login 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


db.employee.update( {}, { $unset: {'EmpID':1}},false,true )


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


db.employee.update( {}, { $unset: {'EmpID':1}},false,true )


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




{ “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


 ddb.employee.updateOne( {}, { $unset: { EmpID:"" }})


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 remove the specified field if matches with a field for field name as “John”.


 db.employee.updateOne( {Name:"John"}, { $unset: { EmpID:"" }})


db.employee.updateOne( {}, { $unset: { EmpID:”” }})
{ “acknowledged” : true, “matchedCount” : 1, “modifiedCount” : 1 }

If you are using an embedded document or in an array, use  for specifying 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.

➤ Use the Select your language drop-down menu in the upper-right to set the language of the following examples.

Delete All Documents

Delete All Documents that Match a Condition

Delete Only One Document that Matches a Condition

Delete Behavior


Delete operations do not drop indexes, even if deleting all documents from a collection.


All write operations in MongoDB are atomic on the level of a single document. For more information on MongoDB and atomicity, see Atomicity and Transactions.

Write Acknowledgement

With write concerns, you can specify the level of acknowledgement requested from MongoDB for write operations. For details, see Write Concern.

How to delete a field in MongoDB using mongoose?

MongoDB provides the $unset operator that deletes a particular field from a document. If the field does not exist in the document, the $unset operator does nothing.

How to use deleteOne in MongoDB?

deleteone() The deleteOne() method deletes the first document from the collection that matches the given selection criteria. It will delete/remove a single document from the collection. It takes four parameters, the first parameter is the selection criteria and the others are optional.

How do I remove a field from an array in MongoDB?

To remove an element, update, and use $pull in MongoDB. The $pull operator removes from an existing array all instances of a value or values that match a specified condition.

How do I delete a post in MongoDB?

To delete a record, or document as it is called in MongoDB, we use the deleteOne() method. The first parameter of the deleteOne() method is a query object defining which document to delete. Note: If the query finds more than one document, only the first occurrence is deleted.