Updating Single Document in MongoDB

Updating Single Document in MongoDB with Examples

In this article, I am going to discuss Updating Single Document in MongoDB with Examples. Please read our previous article where we discussed Embedded Documents in MongoDB with Examples.

Updating Single Document in MongoDB

To update a single document MongoDB provides a method named as updateOne(). This method is used to update one document at a time that first matches the given condition in the specified collection.

This method only accepts a document that consists of an update operation expression. It can also be used in multi-document transactions. While updating the document the value of the _id field remains the same. Starting from MongoDB 4.2, the updateOne() method can also support an aggregation pipeline that specifies the modification. It contains the following stages:

  1. $addFileds and it’s an alias that is $set
  2. $project and it’s an alias that is $unset
  3. $replaceRoot and its allies that is $replaceWith

With the help of an aggregation pipeline, we are allowed for a more expressive update statement, for example, expressing conditional updates based on current field values, etc.

When the upset:true and no document match the specified collection, then this method adds new a new document according to the filter criteria and updates modifications.

In the capped collection, if the update operation changes the size of the document, then the operation will fail. You can also use updateOne() method with the shared collection. This method is not compatible with db.collection.explain(). This method is also used inside multi-document transactions.

Syntax:

db.collection.updateOne(
   <filter>,
   <update>,
   {
      upsert: <boolean>,
      writeConcern: <document>,
      collation: <document>,
      arrayFilters: [ <filterdocument1>, <filterdocument2>… ],
      hint: <document|string>
   }
)

Parameters:

UpdateOne() method has the following parameters:

Parameter Type Description
Filter Document This parameter specifies the selection criteria for the update. If the value of this parameter is empty, then this document updates the very first document of the collection.
Update Document or pipeline This parameter contains the modifications that are going to apply to the document. It can be:
Update document: It consists only of update operator expressions.
Aggregation Pipeline: It consists of the aggregation stages.
Upsert Boolean It is optional and the default value is false.
Value is true, this method: Create a new document if no document matches the specified filter. If the match is found, then update the single document.
Value is false, this method: Does not insert a new document when no match is found.
writeConcern Document It is optional. A document expressing the written concern. Try to omit using the default write concern.
Collation Document It is Optional. It specifies the collation to use for the operation. Collation allows users to specify language-specific rules for string comparison.
arrayFilters Array It is optional. An array of filter documents is used to determine which array elements to modify for an update operation on an array field.
Hint Document It is optional. A document or a string that specifies the index to use to support query predicate. If the specified index does not exist then this operation gives an error.
Return Value
  • updateOne() document returns the following document:
  • matchCount consists of the number of matched documents.
  • modifiedCount consists of the number of modified documents.
  • upsertedId consists of the _id for the upserted document.
  • Acknowledged as true if the operation run with write concern. Otherwise, return false.
Examples to Understand How to Update Single Document in MongoDB

In the following examples, we are going to use:
Database: dotnettutorial
Collection: course

Examples to Understand How to Update Single Document in MongoDB

Updating First Document in MongoDB

In the below example, we update the first document of the course collection.
db.coruse.updateOne({}, {$set:{course: 80}})
Here we use the $set operator to update the value. Also to update the very first document we use an empty document({})

Updating First Document in MongoDB

Updating First Document in MongoDB

Now we update the first document that matches the specified filter.
db.coruse.updateOne({name: “C++”}, {$set:{course: 80}})
Here, we update the first document that matches the filter that is named: “C++”. In this document, we update the value of the course field from 70 to 80.

Updating First Document in MongoDB with Examples

Updating Multiple Fields of a Document in MongoDB

In this example we update the multiple fields of the single document:
db.coruse.updateOne({name: “HTML”}, {$set:{duration: 609, course: 90}})
Here, we update the fields of a document whose name is HTML. In this document, we update multiple fields:
Duration: 609 and course: 90.

Update using upsert in MongoDB

In this example, we set the value of upsert to true:
db.coruse.updateOne({name: “MongoDB”}, {$set:{duration: 609, course: 90}}, {upsert:true})
Here, no document matches the filter(i.e., {name: “MongoDB”}) and the value of upsert is true. So, the updateOne() function adds a document to the course collection.

Updating Single Document in MongoDB with Examples

In the next article, I am going to discuss Updating Multiple Documents in MongoDB with Examples. Here, in this article, I try to explain Updating Single Documents in MongoDB with Examples. I hope you enjoy this Updating Single Document in MongoDB with Examples article.

Leave a Reply

Your email address will not be published. Required fields are marked *