Open In App

MongoDB – FindAndModify() Method

Last Updated : 17 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The findAndModify() method in MongoDB modifies and returns a single document that matches the given criteria. By default, db.collection.findAndModify(document) method returns a pre-modification document.

To return the document with the modifications made on the update, use the new option and set its value to true. It takes a document as a parameter.

Important Points:

  • If you want to find fields of the embedded document, then use the following syntax:

“field.nestedfieldname”: <value>

or

{field: {nestedfieldname: <value>}}

  • The document returned by this method always contains the _id field. If you don’t want the _id field, then set _id:0 in the projection.
  • You can use this method in multi-document transactions.
  • If you are using this method, in shared collection, then the query expression must contain an equality condition on the shared key.

MongoDB FindAndModify() Method Sntax

db.Collection_name.findAndModify(
{
    selection_criteria:<document>,
    sort: <document>,
    remove: <boolean>,
    update: <document>,
    new: <boolean>,
    fields: <document>,
    upsert: <boolean>,
    bypassDocumentValidation: <boolean>,
    writeConcern: <document>,
    collation: <document>,
    arrayFilters: [ <filterdocument1>, ... ]
})

Parameters:

  • remove: It is a must if the update field does not exist. If it is true, removes the selected document. The default value is false.
  • update: It is a must if the remove field does not exist. Performs an update of the selected document. The update field employs the same update operators or field: value specifications to modify the selected document.
  • Others are optional.

Optional Parameters:

  • selection_criteria: It specifies the selection criteria for the modification. The query field employs the same query selectors as used in the db.collection.find() method. Although the query may match multiple documents, findAndModify() will only select one document to modify.
  • sort: Determines which document the operation will modify if the query selects multiple documents. findAndModify() will modify the first document in the sort order specified by this argument.
  • new: When true, returns the modified document rather than the original. The findAndModify() method ignores the new option for remove operations. The default is false.
  • fields: A subset of fields to return. The fields document specifies an inclusion of a field with 1, as in the following:
    • fields: { <field1>: 1, <field2>: 1, … }
  • Upsert: The default value of this parameter is false. When it is true it will make a new document in the collection when no document matches the given condition in the update method.
  • writeConcern: It is only used when you do not want to use the default write concern. The type of this parameter is a document.
  • Collation: It specifies the use of the collation for operations. It allows users to specify the language-specific rules for string comparison like rules for lettercase and accent marks. The type of this parameter is a document.
  • arrayFilters: It is an array of filter documents that indicates which array elements to modify for an update operation on an array field. The type of this parameter is an array.

Return:

  • Returns a single pre-modification document. To return the document with the modifications made on the update, use the new option and set it to true.
  • If no document is found for an upsert, which means the command performs an insert, new is false, and the sort option is NOT specified, the method returns null.
  • If no document is found for an upsert, which means the command performs an insert, and new is false, and a sort option is specified, the method returns an empty document {}.

MongoDB FindAndModify Method Examples

Let’s look at some MongoDB FindandModify examples. In the following examples, we are working with:

Database: gfg

Collections: student

Document: Three documents contains name, language they use in coding and the score they got by solve problem on gfg.

demo database and collection

Update Value using FindandModify Method Example

We are increasing marks by 4 whose name is vishal.

Query:

db.student.findAndModify({query:{name:"vishal"},update:{$inc:{score:4}}})

Output:

value before query

After update:

findandmodify method example

Get the Modified Document after the FindandModify Method Example

 db.student.findAndModify({query:{name:"vishal"},
                          update:{$inc:{score:4}},new:true})

Here, we are again increasing the value of the score field by 4 of the document whose name is “vishal” but this time we set the value of new to true, and the findAndModify() method returns the modified document.

value before query

After update:

fied document after the findandmodify method example output


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads