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 updateOne
Secondary Sidebar
MongoDB vs Postgres

Oracle Java

Data Analysis Tools

MongoDB vs Cassandra

Data Structure Interview Questions

Career In Devops

Mongodb updateOne

Mongodb updateOne

Definition of Mongodb updateOne

MongoDB updateOne method is used to update only one document from the collection, if we wish to update only a single document same time we have using the updateOne method in MongoDB. Using the updateOne method we can update the first matching document, a single document, embedded documents, and also we are able to update single array elements. To update any single document using the updateOne method we need to use the $set operator to replace the present value with new value. Basically, MongoDB updateOne updates the first matching documents from the collection, it will ignore the another matching documents from the collection.

Syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Below is that the syntax of the updateOne method in MongoDB.

db.name_of_collection.updateOne (
{Selection criteria of update query}, {$set operator}
{Upsert (optional), Writeconcern (Optional), collation (optional), arrayFilters (Optional), Hint (optional)}
)

Below is that the parameter description of the above syntax.

1) Name of the collection – This parameter is defined as collection name which was we have used with the updateOne method to update the one documents.

2) UpdateOne – This can be a method name used in MongoDB to update single documents within the collection.

3) Selection criteria of update query – In this section, we have defined the selection criteria of updated documents. Which documents we have updating this is defined in this selection criteria.

4) $set operator – This operator is used in MongoDB while updating any documents. It will update or replace the existing value with a new value.

5) Upsert – It is an optional parameter used with updateOne method in MongoDB. To use this parameter with updateOne method we need to define this parameter value as true in our query. Basically, a default value of this parameter is false in MongoDB.

6) Writeconcern – We can only use this parameter when we didn’t require the default value of this parameter.

7) Collation – This is also the default parameter used with updateOne method in MongoDB. This parameter will specify the use of collation operations in MongoDB. While using this it will allow the language-specific rules to the user.

8) ArrayFilters – The type of this parameter in the updateOne method is an array. This parameter is shown which array we have modifying using the updateOne method.

9) Hint – This specifies that we have using an index or not to filter the data. This is also an optional parameter using the updateOne method in MongoDB.

How updateOne works in Mongodb?

MongoDB update method is basically used to update a single document from the collection in MongoDB.

Using MongoDB updateMany method we can update many documents in the same query, but while using updateOne method we can update a single document from the collection.

The below example shows that using updateOne method we can update only one document from the collection in MongoDB.

db.MongoDB_Update.find ()
db.MongoDB_Update.updateOne ({name: “ABC"}, {$set: {name: "BCD"}})
db.MongoDB_Update.find ()

Mongodb updateOne 1

Figure: Using MongoDB updateOne method we can update only one document from the collection.

In the above example, we can see that ABC names have three documents in MongoDB_Update collection. While using updateOne method only one document is updated which was comes first in order.

In the above example document ID, 1 is updated, because the sequence of this document’s id is first in the MongoDB_Update collection.

MongoDB updateOne method will update only 1 document and skips another document even it will match the specified criteria which was we have defined in our query.

The output of the updateOne method will show the below parameters.

1) Acknowledged.
2) Matched count
3) Modified count

The result of acknowledged is shown the true value while giving an output of updateOne method.

The matched count is shown as how many documents match with the given specified criteria. If we have found any matched value then this parameter shows the output as 1 else it shows the output as zero.

If the matched count matches any documents using the given criteria and if any value is updated using updateOne method then the value of this parameter is 1.

If we have not found any matched value using the given criteria the value of this parameter is zero.

Below example shows how matched count and modified count parameter shows the output in updateOne method

db.MongoDB_Update.updateOne ({name:"PQR"}, {$set: {name: "BCD"}})
db.MongoDB_Update.updateOne ({name:"CBD"}, {$set: {name: "BCD"}})

Mongodb updateOne 2

Figure – how matched count and modified count parameter shows the output in updateOne method.

In the first example, the updateOne method has found the matched result and updated the single documents, so it will show the matched count and modified count as 1.

But in the second example, updateOne method has not found the matched result and has not updated any documents, so it will show the matched count and modified count as zero.

Example

Below example shows updateOne method in MongoDB.

1) Update single document using updateOne method in MongoDB

In the below example, we have updated the name as BCD to MNP. We can see that the first name contains BCD is updated with the name MNP.

Code:

db.MongoDB_Update.find ()
db.MongoDB_Update.updateOne ({name:"BCD"}, {$set: {name: "MNP"}})
db.MongoDB_Update.find ()

Mongodb updateOne 3

Figure – Example to update single document using updateOne method.

2) Update embedded document using updateOne method

In the below example, we have updated the embedded documents. We have updated the lap_spec set for the name as ABC.

Code:

db.MongoDB_Update.find ()
db.MongoDB_Update.updateOne ({name: "ABC"}, {$set: {"lap_spec.ram": 32, "lap_spec.screen": 18.5, "lap_spec.cpu": 1.66}})
db.MongoDB_Update.find ()

Mongodb updateOne 4

Figure – Example to Update embedded document using updateOne method .

3) Update array elements using updateOne method

In the below example, we have updated the array elements documents. We have updated the lap_storage array for the name ABC.

Code:

db.MongoDB_Update.find ()
db.MongoDB_Update.updateOne ({name: "ABC"}, {$set: {"lap_storage.0": 32, "lap_storage.1": 64, "lap_storage.2": 128, "lap_storage.3": 256, "lap_storage.4": 512, "lap_storage.5": 1024}})> db.MongoDB_Update.find ()
db.MongoDB_Update.find ()

Mongodb updateOne 5

Figure – Example to Update array element document using updateOne method in MongoDB.

4) Update the first document using updateOne method

In the below example, we have updated the first document with the name MNP to ABC. We can see that the first name contains MNP is updated with the name ABC.

Code:

db.MongoDB_Update.find ()
db.MongoDB_Update.updateOne ({name: “MNP"}, {$set: {name: "ABC"}})
db.MongoDB_Update.find ()

output 1

Figure – Example to update the first document using updateOne method in MongoDB.

Conclusion

MongoDB updateOne method is used to update single documents from the collection. Using the updateOne method in MongoDB we can update embedded as well as array element documents from the collection. Basically, updateOne method updates the first document from the selection criteria and skip another document.

Recommended Articles

This is a guide to Mongodb updateOne. Here we discuss the definition, How updateOne works in Mongodb? along with examples respectively. You may also have a look at the following articles to learn more –

  1. MongoDB Join
  2. MongoDB findOne()
  3. MongoDB group by
  4. MongoDB Database
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 (17 Courses, 8+ Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
SQL Training Program (10 Courses, 8+ Projects)4.7
Oracle DBA Database Management System Training (2 Courses)4.7
MS SQL Training (16 Courses, 11+ Projects)4.7
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

© 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

*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