EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB Update
Secondary Sidebar
MongoDB Tutorial
  • Commands
    • Mongodb updateMany
    • MongoDB Aggregation
    • Mongodb unwind
    • Mongodb where
    • MongoDB BSON
    • MongoDB Filter
    • Mongodb Match
    • MongoDB sort by date
    • MongoDB Limit()
    • MongoDB Atlas Login
    • MongoDB Relational Database
    • MongoDB count
    • MongoDB Aggregate
    • MongoDB Distinct
    • MongoDB Unique
    • MongoDB find
    • MongoDB findOne()
    • MongoDB insert
    • MongoDB Delete
    • MongoDB Update
    • Lookup in MongoDB
    • order by in MongoDB
    • MongoDB $regex
    • MongoDB $elemMatch
    • MongoDB ObjectId()
    • MongoDB Skip()
    • MongoDB findAndModify
    • Mongodb findOneAndUpdate
    • MongoDB Date Query
    • MongoDB Timestamp
    • MongoDB sort()
    • MongoDB group by
    • MongoDB Join
  • Basics
    • What is MongoDB
    • How To Install MongoDB
    • MongoDB Tools
    • MongoDB GUI Tools
    • MongoDB Versions
    • MongoDB Commands
    • Advantages of MongoDB
    • MongoDB Features
    • Is MongoDB NoSQL
    • Is MongoDB Open Source
    • Build Web Applications using MongoDB
    • MongoDB Data Types
    • MongoDB Administration
    • Data Modeling in MongoDB
    • MongoDB vs Elasticsearch
    • MariaDB vs MongoDB
    • Firebase vs MongoDB
  • Advanced
    • MongoDB Array
    • PostgreSQL ARRAY_AGG()
    • Indexes in MongoDB
    • MongoDB create Index
    • MongoDB Collection
    • MongoDB List Collections
    • MongoDB Capped Collections
    • MongoDB Delete Collection
    • Mongodb show collections
    • MongoDB Auto Increment
    • MongoDB Triggers
    • MongoDB Projection
    • Replication in MongoDB
    • MongoDB Database
    • Mongo DB Create Database
    • MongoDB Compass
    • MongoDB Users
    • MongoDB Authentication
    • MongoDB GridFS
    • MongoDB Relationships
    • MongoDB MapReduce
    • MongoDB Geospatial
    • MongoDB Monitoring
    • Backup in MongoDB
    • MongoDB Sharding
    • MongoDB Java Drivers
    • MongoDB Import
    • Mongo Database Interview Questions
    • MongoDB Join Two Collections
    • MongoDB Group by Multiple Fields
    • MongoDB Pagination
    • MongoDB Replica Set
    • MongoDB Bulk Update
    • MongoDB greater than
    • MongoDB Encryption
    • MongoDB find in array
    • MongoDB like query
    • Mongodb shell
    • MongoDB port
    • MongoDB Query Operators
    • MongoDB Web Interface
    • MongoDB Query Array
    • MongoDB Transactions
    • MongoDB Not In
    • MongoDB not null
    • MongoDB npm
    • MongoDB Remove

Related Courses

MongoDB Certification Course

Oracle Certification Course

All in One Data Science Course

SQL Training Course

Oracle DBA Course

MS SQL Certification Course

MongoDB Update

By Priya PedamkarPriya Pedamkar

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.

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,112 ratings)

Syntax

Below is the syntax of the update method:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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?
Popular Course in this category
MongoDB Training Program (4 Courses, 2 Projects)
  4 Online Courses |  2 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Oracle Training (14 Courses, 8+ Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
SQL Training Program (7 Courses, 8+ Projects)4.7
Oracle DBA Database Management System Training (2 Courses)4.7
MS SQL Training (16 Courses, 11+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • 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

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

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

EDUCBA Login

Forgot Password?

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

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

EDUCBA

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

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

Let’s Get Started

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