EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • 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
  • Login
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB Update

MongoDB Update

Priya Pedamkar
Article byPriya Pedamkar

Updated February 28, 2023

MongoDB Update

Introduction to MongoDB Update

MongoDB Update method is used to update the document from the collection, and the update method will update the value of the existing document. We have used a $set operator at the time of updating the document. We can update a single document by using an update or updateOne method. If we want to update multiple documents in a single update command, we must update many methods. Using the update method, we can update a single as well as multiple documents in one statement. The update method is essential and useful in MongoDB to update the document.

ADVERTISEMENT
Popular Course in this category
MONGODB Course Bundle - 6 Courses in 1

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax

Below is the syntax of the update method:

collection_name.update (document)

OR

collection_name.update (< query>, <update>)
{
Upsert:  <Boolean>
Multi: <Boolean>
Writeconcern:  <document>
Collation: <document>
ArrayFilters:  [<filterdocument1>, …] } )

OR

collection_name.updateOne (Document) – Update single document

OR

collection_name.updateMany (Multiple Documents) – Update multiple document

OR

collection_name.findOneAndUpdate (Selection_critera_of_document, Updated_data_of_document)

Parameter description of MongoDB Update

  • Collection name: Collection name is defined as update documents from the collection by using the update method. We can update single and multiple documents at one time using updateOne and update many methods.
  • Document: Document is defined as data that we have to update collection by using an update method. We can update single or multiple documents at one time.
  • Write concern: This is an optional parameter of the update method.
  • Query: Query defines as a selection criterion of a document for the update operations. Using the query, we have to define select criteria of update.
  • Update: Update method is used to update the document from the collection; the update method will update the value of the existing document. The update method is essential and useful.
  • Upsert: It is an optional parameter of the update method. It will create a new document when our updated query is not matched with the existing document.
  • Multi: It will update multiple documents from a single query when our query matches the criteria. It is an optional parameter of the update method.
  • Collation: Collation is used to allow users to specify language-specific rules for the string comparison. It is an optional parameter of the update method.
  • Array Filters: It is an optional parameter of the update method. It will filter an array that specifies which documents elements modify an update operation in the array field.
  • Update one: Update one method is used to update a single document. If we want to update a single document, then we have using the update method.
  • Update Many: Update many methods is used to update multiple documents. If we want to update multiple documents in a single statement, we have used many methods.
  • Find one and update: This method is used to find and update the document.

How Update command works in MongoDB

Below is the working of the update command in MongoDB.

  • The update method is essential and useful in MongoDB to update the document.
  • Using the update method, we can update single and multiple documents in one statement in MongoDB.
  • We can update a single document by using an update or updateOne method. If we want to update multiple documents in a single update command, we have to update many MongoDB methods.
  • MongoDB update method is used to update the document from the collection; the update method in MongoDB will update the existing document’s value.
  • We have used a $set operator at the time of updating the document.
  • While using the Upsert method in MongoDB, it will insert the document when the document is not present in the collection. The collection is also automatically created at the time of document updating using the Upsert method in MongoDB.
  • The below example shows the Upsert method will automatically insert the document when this is not present in the collection.

use db_update

Database update

db.books_update.update ({item: "1"},{book_name: "story", stock: 5, tags: ["database"]}, {upsert: true})

Story Book

db.books_update.find()

Object Id

db.books_update.update ({item: "2"},{book_name: "story_book", stock: 10, tags: ["database"]}, {upsert: true})

Update Book

db.books_update.find()

Books update

Explanation: In the first example, we have inserted a document using an update and upsert method in MongoDB. At the time of insertion, we have created a books_update collection.

Examples to Implement MongoDB Update

Below is the example of an update method in MongoDB. We have taken an example of an emp_count table to describe examples of MongoDB’s update method as follows. Below is the data description of the emp_count table are as follows.

Code:

use emp_count
db.emp_count.find ()

Output:

method in MongoDB

Example #1

Update single documents using the update method: In the below example, we have updated a single document using the update method. We have updated emp_id value from 6 to 101 by using an update method in MongoDB.

Code:

db.emp_count.update({'emp_id': 6},{$set:{'emp_id': 101}})
db.emp_count.find ()

Output:

single document

Example #2

Update single documents using the updateOne method: In the below example, we have updated a single document using the update method. We have updated the emp_id value from 5 to 100 by using the update method.

Code:

db.emp_count.updateOne({'emp_id': 5}, {$set: {'emp_id': 100}})
db.emp_count.find ()

Output:

updateOne method

Example #3

Update multiple documents using the update many methods: In the below example, we have updated multiple documents using the update many methods. We have updated emp_name value from XYZ to CBA by using the update many methods in MongoDB.

Code:

db.emp_count.updateMany ({'emp_name': "XYZ"}, {$set:{'emp_name': "CBA"}})>
db.emp_count.find ()

Output:

update many method

Conclusion

MongoDB update method is used to update the document from the collection; the update method in MongoDB will update the existing document’s value. We have used a $set operator at the time of updating the document. The update method is essential and useful in MongoDB to update the document.

Recommended Articles

This is a guide to MongoDB Update. Here we discuss Introduction to MongoDB Update, Syntax, How does it work, Examples with code and output. You can also go through our other related articles to learn more –

  1. MongoDB Commands
  2. MongoDB List Collections
  3. MySQL vs MongoDB
  4. MongoDB Unique | Examples
  5. MongoDB vs Elasticsearch with Infographics
  6. Guide to MongoDB Projection
  7. MongoDB insert | How to Work?
ADVERTISEMENT
C++ PROGRAMMING Course Bundle - 9 Courses in 1 | 5 Mock Tests
40+ Hour of HD Videos
9 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
ASP.NET Course Bundle - 28 Courses in 1 | 5 Mock Tests
123+ Hours of HD Videos
28 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
SQL Course Bundle - 51 Courses in 1 | 6 Mock Tests
205+ Hours of HD Videos
51 Courses
6 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
SOFTWARE TESTING Course Bundle - 13 Courses in 1
53+ Hour of HD Videos
13 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - 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

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

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

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

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Extended Cyber Monday Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW