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 findAndModify
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 findAndModify

By Priya PedamkarPriya Pedamkar

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.

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 and Parameters

Below is the syntax of the findAndModify method in MongoDB.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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?
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