![]() |
MongoDB update operators offer powerful tools for modifying documents within a collection which provides flexibility and efficiency in database operations. From setting and unsetting fields to manipulating arrays and applying bitwise operations. In this article, we will understand different Update Operators which is used in MongoDB. Update Operators
1. FieldsMongoDB allows us to update the individual fields within a document.
2. ArraysThe following modifiers are useful for manipulating array data.
3. ModifiersMongoDB offers modifiers that bring additional functionality to the update operation.
4. Bitwise
Example: Update OperatorsExample 1: Using $currentDateImagine you have a collection of blog posts, and when editing. This timestamping operation can be assisted by the $currentDate operator. // Before Update Output: // Before Update Before Updates// Retrieve a blog post with a specific ID Before doing any editing, this line first gets a blog post from the MongoDB collection named blogPosts. It does this by looking for a document with the specific _id (a unique identifier), which each blog post has. Update using $currentDate// Update the "lastModified" field to the current date and time This line updates a particular post in the blogPosts collection. This update is focused on the document with a given _id (unique identifier). The update operation uses the $currentDate operator to set its value of lastModified field into current date and time. In fact, it’s like adding a time stamp to the blog post showing when it was last modified. After Update// Retrieve the updated blog post Following the update, this line retrieves again to observe how things have changed. Now, the lastModified field should be set to today’s date and time so that people can see when this blog post was last altered. Example 2 : Using $inc and $setConsider a scenario where you have a collection of products, and you want to update the inventory count and set a new discount for a specific product. // Before Update Output: // Before Update Before Updates// Retrieve product information with a specific ID This line retrieves a product document from the MongoDB collection products. It seeks a document with exactly the same_id as productId. Update using $inc and $set// Update the product with specified changes This block updates a particular product in the products collection. The product is identified by its unique_id. The update operation uses two operators:
After Updates// Retrieve the updated product information Following the update, this line retrieves the same product to see the changes. Now, the inventory should be reduced by 5 units, and the discount should reflect the new value of 0.1. This code is effectively updating a product’s information in MongoDB. It decreases the inventory by 5 units and sets a new discount of 10%, providing a practical example of how to modify data in a MongoDB collection. ConclusionMongoDB Updatе Opеrators is еffеctivе data managеmеnt. Thе operators providеs a powerful toolkit for your data to meet evolving requirements. Whether it’s updating fields, arrays, or introducing new elements, MongoDB’s updatе capabilitiеs offеr flexibility and precision. FAQs on MongoDB Update OperatorsHow does the $set operator function in MongoDB updates?
How does the
|
Reffered: https://www.geeksforgeeks.org
Databases |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |