Update Document: 

In MongoDB shell, the updateOne() and updateMany() methods are used to update documents in a collection. 

They are specialized methods that provide a convenient way to update a single document or multiple documents that match a specified criteria, respectively. 

Let's go through each method and their usage:


updateOne() method:

The updateOne() method updates a single document that matches the specified criteria in a collection. If multiple documents match the criteria, only the first document encountered will be updated.

Syntax:

db.collectionName.updateOne(filter, update, options)
  • collectionName: is the name of the collection you want to update.
  • filter: is a document that specifies the criteria for matching the document to be updated.
  • update: is a document that defines the modifications to be made to the matched document.
  • options: is an optional document that specifies additional update options.

Example:

db.myCollection.updateOne({ _id: ObjectId("12345") }, { $set: { name: "John" } })

This query updates the document with the _id value of "12345" in the "myCollection" collection and sets the "name" field to "John". Only the first document that matches the criteria will be updated.

  1. updateMany() method: The updateMany() method updates multiple documents that match the specified criteria in a collection. It applies the update operation to all matching documents.

Syntax:

db.collectionName.updateMany(filter, update, options)
  • collectionName is the name of the collection you want to update.
  • filter is a document that specifies the criteria for matching the documents to be updated.
  • update is a document that defines the modifications to be made to the matched documents.
  • options is an optional document that specifies additional update options.

Example:

db.myCollection.updateMany({ age: { $gt: 25 } }, { $inc: { salary: 5000 } })

This query updates all documents in the "myCollection" collection where the "age" field is greater than 25. It increments the "salary" field by 5000 for each matched document.

Both updateOne() and updateMany() methods support various update operators, such as $set, $inc, $unset, and more, which allow you to modify specific fields of the documents.

Additionally, you can use options like upsert to insert a new document if no match is found or writeConcern to control the write acknowledgment level.

These methods provide a straightforward way to update documents in MongoDB shell based on your specific needs, whether you want to update a single document or multiple documents in a collection

Insert If Record Not Found:

Suppose you have a collection called "users" with documents representing users. Each document has an _id field and a name field. We want to update a document with a specific _id, and if the document does not exist, insert a new document with the provided _id and name.


db.users.updateOne( { _id: ObjectId("12345") }, { $set: { name: "John" } }, { upsert: true } )

In this example:

  • The updateOne() method is called on the "users" collection.
  • The first argument is the filter specifying the criteria for matching the document. In this case, we are looking for a document with the _id value of "12345".
  • The second argument is the update operation. We use the $set operator to set the "name" field to "John" for the matched document.
  • The third argument is an options document. We specify upsert: true to indicate that if no matching document is found, a new document should be inserted with the provided _id and name.

If a document with the _id value of "12345" exists, its "name" field will be updated to "John". If no document matches the specified criteria, a new document will be inserted with the provided _id and name fields.

The upsert option is useful when you want to update a document if it exists or insert it as a new document if it doesn't exist. It provides a convenient way to perform an update operation with automatic upsert behavior.