Learning MongoDB

0% completed

Previous
Next
Update Operations

Update operations in MongoDB allow you to modify existing documents within a collection. MongoDB provides several methods for updating documents, each suited to different use cases.

These methods include:

  • update()
  • updateOne()
  • updateMany()
  • replaceOne().

Understanding these methods and how to use them effectively is crucial for maintaining and manipulating your MongoDB data.

update() Method

The update() method is used to modify existing documents in a collection. This method was more commonly used in older versions of MongoDB but has been largely replaced by updateOne() and updateMany() in modern applications. The update() method allows you to specify a filter to select the documents to be updated and an update document that defines the modifications to be applied.

Syntax

db.collection.update( <filter>, <update>, { upsert: <boolean>, multi: <boolean>, writeConcern: <document> } )
  • filter: Specifies the criteria to select the documents to update.
  • update: Defines the modifications to apply.
  • upsert (optional): If true, creates a new document if no document matches the filter.
  • multi (optional): If true, updates multiple documents that match the filter.
  • writeConcern (optional): Specifies the write concern for the operation.

Examples

  1. Basic Update

    db.users.update({ name: "Alice" }, { $set: { age: 26 } })

    This command updates the document where the name is "Alice", setting the age field to 26.

  2. Updating Multiple Fields

    db.users.update({ name: "Bob" }, { $set: { age: 31, email: "bob.new@example.com" } })

    This command updates the document where the name is "Bob", setting both the age field to 31 and the email field to "bob.new@example.com".

  3. Upsert Option

    db.users.update({ name: "Dave" }, { $set: { age: 40 } }, { upsert: true })

    This command updates the document where the name is "Dave". If no such document exists, it inserts a new document with name as "Dave" and age as 40.

updateOne() Method

The updateOne() method is used to update a single document that matches the specified filter. It is part of MongoDB's new CRUD API introduced in version 3.2, providing more precise control and better performance for single-document updates. This method ensures that only the first matching document is updated.

Syntax

db.collection.updateOne( <filter>, <update>, { upsert: <boolean>, writeConcern: <document> } )
  • filter: Specifies the criteria to select the document to update.
  • update: Defines the modifications to apply.
  • upsert (optional): If true, creates a new document if no document matches the filter.
  • writeConcern (optional): Specifies the write concern for the operation.

Examples

  1. Basic Update

    db.users.updateOne({ name: "Alice" }, { $set: { age: 27 } })

    This command updates the first document where the name is "Alice", setting the age field to 27.

  2. Updating Nested Fields

    db.users.updateOne({ name: "Bob" }, { $set: { "address.city": "New City" } })

    This command updates the city field of the address object for the first document where the name is "Bob".

  3. Using Array Filters

    db.users.updateOne({ name: "Charlie" }, { $set: { "tags.$": "updatedTag" } })

    This command updates the first element in the tags array to "updatedTag" for the first document where the name is "Charlie".

  4. Upsert Option

    db.users.updateOne({ name: "Eve" }, { $set: { age: 45 } }, { upsert: true })

    This command updates the first document where the name is "Eve". If no such document exists, it inserts a new document with name as "Eve" and age as 45.

updateMany() Method

The updateMany() method is used to update multiple documents that match the specified filter. This method is efficient for bulk updates and ensures that all matching documents are updated according to the provided update document.

Syntax

db.collection.updateMany( <filter>, <update>, { upsert: <boolean>, } )
  • filter: Specifies the criteria to select the documents to update.
  • update: Defines the modifications to apply.
  • upsert (optional): If true, creates new documents if no documents match the filter.

Examples

  1. Basic Update

    db.users.updateMany({ age: { $lt: 30 } }, { $set: { isActive: true } })

    This command updates all documents where the age field is less than 30, setting the isActive field to true.

  2. Incrementing Field Values

    db.users.updateMany({}, { $inc: { age: 1 } })

    This command increments the age field by 1 for all documents in the users collection.

replaceOne() Method

The replaceOne() method is used to replace a single document that matches the specified filter with a new document. Unlike the update methods, replaceOne() replaces the entire document rather than modifying specific fields. This method is useful when you need to completely overwrite an existing document.

Syntax

db.collection.replaceOne( <filter>, <replacement>, { upsert: <boolean>, writeConcern: <document> } )
  • filter: Specifies the criteria to select the document to replace.
  • replacement: The new document that will replace the existing document.
  • upsert (optional): If true, creates a new document if no document matches the filter.
  • writeConcern (optional): Specifies the write concern for the operation.

Examples

  1. Basic Replace

    db.users.replaceOne({ name: "Alice" }, { name: "Alice", age: 28, email: "alice.new@example.com" })

    This command replaces the first document where the name is "Alice" with a new document containing the specified fields.

  2. Replacing with Additional Fields

    db.users.replaceOne({ name: "Bob" }, { name: "Bob", age: 31, email: "bob.new@example.com", address: { city: "New City", zipcode: "00000" } })

    This command replaces the first document where the name is "Bob" with a new document containing additional fields for address.

  3. Replacing Entire Document

    db.users.replaceOne({ name: "Charlie" }, { name: "Charlie", age: 36 })

    This command replaces the first document where the name is "Charlie" with a new document, removing any fields that were in the original document but are not in the new one.

  4. Upsert Option

    db.users.replaceOne({ name: "Dave" }, { name: "Dave", age: 41 }, { upsert: true })

    This command replaces the first document where the name is "Dave". If no such document exists, it inserts a new document with name as "Dave" and age as 41.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next