EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB findAndModify
 

MongoDB findAndModify

Priya Pedamkar
Article byPriya Pedamkar

Updated February 28, 2023

MongoDB findAndModify

 

 

Definition of MongoDB findAndModify

MongoDB findAndModify is used to modify the single document from the collections, by default document did not include any modification update method done this modification in MongoDB. Instead of updating if we have used the findAndModify method, then the first document is updated, and the updated document will display as a result. MongoDB findAndModify method is essential and useful to display the updated documents in one query. This method is used to modify and single document based upon selection criteria that we have used in a query, and the return result document will not show the updated document by default.

Watch our Demo Courses and Videos

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

Syntax and Parameters

Below is the syntax of the findAndModify method in MongoDB.

Syntax:

collection_name.findAndModify (Document)

collection_name.findAndModify ({
Query:  <document>
Sort:  <document>
Remove:  <Boolean>
New:  <Boolean>
Upsert:  <Boolean>
Update: <aggregation pipeline or document>
Fields:  <document>
Collation: <document>
ArrayFilters:  [<filterdocument1>, …] } );

Parameter:

Below is the parameter description of MongoDB findAndModify

  • Collection name: Collection name is defined as modify documents from the collection by using the findAndModify method. The collection was automatically created using the findAndModify method when it was not present in the database.
  • Document: Document is defined as data that we have to modify collection using the findAndModify method in MongoDB. We can modify single or multiple documents at one time.
  • Write concern: This is an optional parameter of the findAndModify method.
  • Query: Query defines as a selection criterion of a document for the modify operations in MongoDB. Using the query, we have to define select criteria of findAndModify method in MongoDB.
  • Update: Update will specify the remove or update a field from a document by using the findAndModify method.
  • Upsert: It is an optional parameter of the findAndModify method in MongoDB. It will create a new document when our updated query is not matched with the existing document.
  • Collation: Collation is used to allow users to specify language-specific rules for the string comparison. It is an optional parameter of the findAndModify method in MongoDB.
  • Array Filters: It is an optional parameter of the findAndModify method in MongoDB. It will filter an array that specifies which document elements to modify an update operation in the array field.
  • FindAndModify: findAndModifyis used to modify the single document from the collections, by default document was not include any modification update method is done this modification in MongoDB.
  • Fields: Fields is an optional parameter of the findAndModify method in MongoDB. It will specify that inclusion with 1 from the fields.
  • New: When the return result of the findAndModify method is a modified document, or it was not original, then it will return true.
  • Remove: The default value of removal in findAndModify method is false. It will remove the document which was specified in the query.

How findAndModify Command Works in MongoDB?

Below is the working of the findAndModify method.

  • Using the findAndModify method, we can modify single or multiple statements as per the criteria defined in the query.
  • It is used to modify the single document from the collections.
  • In MongoDB by default document was not include any modification update method is done this modification in MongoDB.
  • While using the Upsert infindAndModify 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 modifying using the findAndModify method in MongoDB.
  • The below example shows the Upsert using findAndModify method will automatically insert the document when this is not present in the collection.

use db_test;

MongoDB findAndModify-1.1

db.emp_test.findAndModify({query: {name: "ABC", emp_id: 1, emp_salary: 10000}, update: {$inc: {emp_phone: 1234567890}}, upsert: true})

MongoDB findAndModify-1.2

show collections

MongoDB findAndModify-1.3

db.emp_test.find()

MongoDB findAndModify-1.4

  • In the above example, emp_test collection was automatically created at the time of modification and insertion of the document using the findAndModify method in MongoDB.
  • We have not used sort to return as the result’s null value, but the document is inserted in emp_test collection.
  • Upsert is an optional parameter of the findAndModify method in MongoDB. It will create a new document when our updated query is not matched with an existing document.
  • This method is essential and useful to display the updated documents in one query.
  • This method is used to modify the single document based upon selection criteria that we have used in a query, and the return result document will not show the updated document by default.
  • Instead of an update if we have used the findAndModify method, then the first document is updated, and the updated document will display as a result.
  • Collation is used in findAndModify method to allow users to specify language-specific rules for the string comparison.

Examples of MongoDB findAndModify

Below is the example of the findAndModify method.

  • We have taken the example of the emp_count table to describe how the findAndModify method in MongoDB is as follows. Below is the data description of the emp_count table are as follows.

db.emp_count.find ()

Output-2.1

Upsert and update using the findAndModify method:

  • Below example show upsert and update using the findAndModify method in MongoDB. We have inserted a new document by using upsert in emp_count table.

db.emp_count.findAndModify({query: { emp_name: "ABC", emp_address: "Pune", emp_phone: 1234567890, emp_salary: 20000 }, sort: { emp_id: 1 }, update: { $inc: { emp_id: 301 } }, upsert: true })

Output-2.2

Sort and remove using the findAndModify method:

  • The below example shows sort and removes using findAndModify method in MongoDB. We have deleted documents from emp_count table where emp_name is ABC.

db.emp_count.findAndModify ({query: {emp_name: "ABC"}, sort: {emp_id: 1},  remove: true })

Output-2.3

Conclusion

It is used to modify the single document from the collections. By default document was not include any modification update method is done this modification in MongoDB. We can insert a new document using the findAndModify method if it’s not present in the collection. This method is important in MongoDB. 

Recommended Articles

This is a guide to MongoDB findAndModify. Here we also discuss how findAndModify command works in MongoDB? Along with different examples and code implementation. You may also have a look at the following articles to learn more –

  1. Indexes in MongoDB
  2. MongoDB Limit()
  3. order by in MongoDB
  4. MongoDB Tools
  5. MongoDB count | How to Works?
  6. MongoDB find | How Finds Command Works?
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
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

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

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 Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW