0% completed
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.
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.
db.collection.update( <filter>, <update>, { upsert: <boolean>, multi: <boolean>, writeConcern: <document> } )
true
, creates a new document if no document matches the filter.true
, updates multiple documents that match the filter.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.
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".
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.
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.
db.collection.updateOne( <filter>, <update>, { upsert: <boolean>, writeConcern: <document> } )
true
, creates a new document if no document matches the filter.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.
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".
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".
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.
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.
db.collection.updateMany( <filter>, <update>, { upsert: <boolean>, } )
true
, creates new documents if no documents match the filter.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
.
Incrementing Field Values
db.users.updateMany({}, { $inc: { age: 1 } })
This command increments the age
field by 1 for all documents in the users
collection.
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.
db.collection.replaceOne( <filter>, <replacement>, { upsert: <boolean>, writeConcern: <document> } )
true
, creates a new document if no document matches the filter.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.
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
.
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.
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.
.....
.....
.....