EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials MongoDB Tutorial Mongodb remove field
 

Mongodb remove field

Updated March 8, 2023

Mongodb remove field

 

 

Introduction to Mongodb remove field

Mongodb remove field is defined as remove the field from MongoDB collection by using the unset operator. Using unset operator we can remove a single field as well as multiple fields in a single command, we can delete fields from collections as per matching documents also we can delete fields from all the documents. If suppose the field is not present in the collection unset operator is not taking any effect on the collection. We can also delete field from the first matching document, it will only delete field from a single document that was matched first in our query.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax

  • Below is the syntax of remove field in MongoDB.
  • Remove single field from collection –

db.name_of_collection.update ( { }, { $unset: { Field_name: "" } } )

  • Remove multiple field from collection –

db.name_of_collection.update ( { }, { $unset: { Field_name1: "", Field_name2: "", Field_name3: "" } } )

  • Remove embedded field from collection using update function –

db.name_of_collection.update ( { }, { $unset: { “embedded_field_name.embedded_field”: "" } } )

  • Remove single field from collection using updateMany function –

db.name_of_collection.updateMany ( { }, { $unset: { Field_name: "" } } )

  • Remove array field from collection using update function –

db.name_of_collection.update ( { }, { $unset: { “array_field_name.array_elements”: "" } } )

Parameter description syntax of remove field in MongoDB.

  • Name of collection – This parameter is defined as collection name from which we have removing the field using the unset operator.
  • Update – This is the method name that was used with the unset operator to update the specified collection. Using the update method and unset operator we have to remove field from collection in MongoDB.
  • Unset – This operator is used in MongoDB to remove the field from the collection. Using unset operator we have to remove single as well as multiple field from the collection by using a single query.
  • Field name 1 to field name N – This is defined as the name of field which was we have used with unset operator to delete from the collection. If suppose we want to delete a single field then we need to define single field in our query with the unset operator. If suppose we want to delete multiple field in a single query then we need to define multiple fields in our query with the unset operator.
  • UpdateMany – This is the method name that was used with unset operator to remove the specified collection documents field. It will delete the multiple documents field from the collection in single query.

How to remove field in Mongodb?

  • We have to remove field from the collection in MongoDB by using the unset operator. We can remove the single field in one query or also we can remove multiple fields in one query by using the unset operator.
  • We have using update, updateMany method with the unset operator to remove field from collection.
  • To remove the embedded documents fields we need to use “.” notation. We can delete multiple embedded fields in single query.
  • If suppose we want to delete field from specified documents we need to define in our query. If suppose we have not define the documents in our query all the documents field will be deleted.
  • If suppose field is not preset into the collections unset operator will not take any action to delete it. It will returns the 0 count of upserted and modified parameters.
  • Below example shows unset operator will not taking any actions when we have using non-matching field. In the below example, we have used Stud_mark field which was not available in remove_field collection, so it will return the result like “nMatched” count is 1, “nUpserted” count is 0, and “nModified” count is 0. Means unset operator not taking any action to remove it.

Code –

db.remove_field.update ( { }, { $unset: { Stud_mark: "" } } )
db.remove_field.find ()

Mongodb remove field output 1

Figure – If suppose field is not preset into the collections unset operator will not taking any action to remove it.

  • We can also remove the array documents field using unset operator in MongoDB. To remove the array documents field we need to specify which field we want to remove from collections.
  • If suppose specified collection is not present in the database unset operator will not take any action on that particular collection.

Code –

db.unset_test.update ( { _id: 1 }, { $unset: { "unset": "" } } )
show collections

Mongodb remove field output 2

Figure – If suppose specified collection is not present in the database unset operator will not take any action.

  • In the above example, we can see that the result of unset operations is 0 of all the parameters.

Example

The below example shows remove_field from collection in MongoDB.

  • Remove single field from collection –
  • In the below example, we have removed single field from collection. We have removed field name as stud_name from remove_field collection.
  • We have used updateMany method to remove the fields from all the documents.

Code –

db.remove_field.find ()
db.remove_field.updateMany ( { }, { $unset: { "stud_name": "" } } )
db.remove_field.find()

Mongodb remove field output 3

Figure – Example of remove single field from the collection by using the unset operator.

Remove multiple field from collection –

  • In the below example, we have removed two field from collection. We have removed field name as stud_name and stud_id from remove_field collection.

Code –

db.remove_field.updateMany ( { }, { $unset: { "stud_name": "", "stud_id": "" } } )
db.remove_field.find ()

output 4

Figure – Example of remove multiple field from collection by using the unset operator.

Remove embedded documents field from collection –

  • In the below example, we have removed embedded documents field from collection. We have removed embedded field name as grades. First and grades.Second from remove_field collection.

Code –

db.remove_field.find ()
db.remove_field.updateMany ( { }, { $unset: { "grades.First": "", "grades.Second": "" } } )
db.remove_field.find ()

output 5

Figure – Example of remove embedded documents field from collection by using the unset operator.

Remove array elements field from collection –

  • In the below example, we have removed the array elements field from collection. We have removed array elements name as marks.0 from remove_field collection.

Code –

db.remove_field.find ()
db.remove_field.updateMany ( { }, { $unset: { "marks.0": "" } } )
db.remove_field.find ()

output 6

Figure – Example of remove array elements field from collection by using the unset operator.

Conclusion

Using unset operator we can remove single as well as multiple field from collection. We can also remove the field of single as well as multiple documents. We can also use unset operator on embedded and array documents field to remove those fields from collections.

Recommended Articles

This is a guide to Mongodb remove field. Here we discuss How to remove the field in Mongodb along with the examples and codes. You may also have a look at the following articles to learn more –

  1. MongoDB Database
  2. MongoDB sort()
  3. MongoDB Monitoring
  4. MongoDB Skip()

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW