MongoDB findAndModify() example

MongoDB findAndModify() method modifies and returns a single document based upon the selection criteria entered. The returned document does not show the updated content by default. If the records matching the criteria does not exist in the database, a new record will be inserted if the upsert is set to true.

MongoDB findAndModify()

The syntax for mongodb findAndModify method is as follows.

The description of the parameters are as follows.

query: Defines the selection criteria as to which record needs modification.

sort: Determines which document should be modified when the selection criteria retrieves multiple documents.

new: indicates that the modified document will be displayed.

fields: specifies the set of fields to be returned.

upsert: creates a new document if the selection criteria fails to retrieve a document.

MongoDB findAndModify important points

Some of the things to keep in mind while using the findAndModify MongoDB calls are:

  • If the entered selection criteria does not fetch any document and if upsert is set to true, then the specified values are inserted and a new document is created.
  • If the entered selection criteria does not fetch any document while performing update or remove operations, the output returned is null.
  • If the new option is set to false and sort operation is not mentioned, the output returned is null.
  • If the new option is set to false and sort operation is specified, the output is empty.

MongoDB findAndModify Example

Now let us see some examples demonstrating the usage of the findAndModify API.

First, let us create some test data to start with.

We will create a new car document with name, color, car no (cno), speed and manufactured country(mfdcountry) fields through the mongo console.

Let us now verify that the data was actually inserted using mongodb find :

Moving on to the actual usage of find and modify, we depict different possibilities.

Case 1: The document exists in the database

  1. The query finds a document in the car collection where the name field has the value Alto.
  2. The sort orders the results of the query in ascending order. If multiple documents meet the query condition, the method will select for modification the first document as ordered by this sort.
  3. The update increments the value of the speed field by 10.
  4. The method returns the original document selected for this update :


Case 2: The new option set to true (returns the updated data set)


Note that speed is being displayed as 45 which is the updated value since we set new to be true. If this is not set, the speed field will be shown as 20 , the old value though it gets updated in the database.

Case 3: The upsert is set to true


The car name with WagonR is not in the db hence a new document is created in database. The method returns an empty document { } if the sort option is specified. If sort option is not included then the method returns null.

Apart from these, this can also be used for doing a Sort and Remove operation.

If the remove field is set to true, the car name with the specified criteria will be removed from the database.


The remove field is set to true the document with the name “Alto” gets deleted from the database.

MongoDB findAndModify Java Example

The operations depicted above is all manual performed using mongo shell. The same can be done programmatically in Java as below.

Download the mongo driver jar and add it to your classpath.

First, we need to establish a connection to the mongodb server using the Mongo client. The name of the database should be specified for the connection as a parameter. If the database does not exists, a new database will is created.

After this, we will add a few records into the database and do a findAndModify operation and then verify if the records are actually updated.

Below program works with mongo java driver versions 2.x.


If you are using MongoDB java driver versions 3.x, then use below program.

Below image shows the output of above program, notice the change in color field.

mongodb findAndModify example

That’s all for MongoDB findAndModify example, we will look into more MongoDB operations in the coming posts.

Reference: MongoDB Official Documentation

By admin

Leave a Reply

%d bloggers like this: