MongoDB Upsert

Introduction

In MongoDB, upsert is a method that is used to insert and update the value in any operation. In other words, the MongoDB upsert method is a combination of insert and update (insert + update = upsert). By default, the upsert method's value is always false. If the document matches the specified query and the method's value is set to true, the update operation will update the matching documents. If the document does not match the specified query and the method's value is set to true, this method inserts a new document in the collection. This new document contains the fields that indicate to the operation.

Syntax:

The value of the upsert option will be either true or false.

Upsert with findAndModify() function:

The user can use the upsert option in conjunction with the findAndModify() function. In this function, the default value of the option is false. If the value of this option is set to true, the function performs one of the following operations:

  1. If a document is found that matches the given query criteria, the findAndModify() function updates the document.
  2. If no document matches the given query criteria, the findAndModify() function inserts a new document into the collection.

Syntax:

For example:

In this example, we are working with



MongoDB upsert

Now we are going to insert a new document in the student collection by setting the upsert option to true.

Here, no data matches with the name "jack", so the findAndModify() function inserts a new document containing two fields (i.e., "name" and "address") because of the value of the upsert method is set to true.

MongoDB upsert

Upsert with update() function:

You can use the upsert option in conjunction with the update() function. In this function, the default value of the option is false. If the value of this option is set to true, the function performs one of the following operations:

  1. If a document is found that matches the given query criteria, the update() function updates the document.
  2. If no document matches the given query criteria, the update() function inserts a new document into the collection.

Syntax:

For example:

In this example, we are working with



MongoDB upsert

Now we are going to insert a new document in the student collection by setting the upsert option to true.

Here, no data matches with the name "Noah", so the update() function inserts a new document containing two fields (i.e., "name" and "address") because the value of the upsert method is set to true.

MongoDB upsert

Upsert with Operator Expressions:

If a document does not match the given data and the value of the upsert option is set to true, the update operation creates new documents from the similarity clause in the query parameter given and applies expressions from the updated parameter.

For example:

In this example, we are working with



MongoDB upsert

Now we will insert a new document in the Employees collection by setting the value of the upsert option to true.

Here, the update() method creates a new document the with field "Name : Hnery" from the query condition and then applies the $set and $setOnInsert operations to this document.

MongoDB upsert




Latest Courses