Open In App

MongoDB – FindAndModify() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The findAndModify() method modifies and return a single document that matches the given criteria. By default, this 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.

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

“field.nestedfieldname”: <value>

or

{field: {nestedfieldname: <value>}}

  • The document return 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.

Syntax:

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 must if update field not exists. If it is true, removes the selected document. The default value is false.
  • update: It is must if remove field not exists. Performs an update of the selected document. The update field employs the same update operators or field: value specifications to modify the selected document.
  • Other 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, and 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 {}.

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.

  • We are increasing marks by 4 whose name is vishal: 
db.student.findAndModify({query:{name:"vishal"},update:{$inc:{score:4}}})

Here, we are increasing the value of marks field of the document whose name is “vishal” using findAndModify() method. This method returns pre modification document

Note: $inc operator increases the value in the score.

After update:

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

Here, we are again increasing the value of 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 return the modified document.

After update:


Last Updated : 05 Feb, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads