@mac
To add a value to an existing field in MongoDB, you can use the $inc
operator with an update operation. Here's an example of how you can add a specific value to an existing field in a MongoDB collection:
1 2 3 4 |
db.collection.update( { _id: ObjectId("document_id") }, { $inc: { field_to_update: value_to_add } } ) |
In this example:
This operation will increment the value of the specified field by the specified value. If the field does not exist in the document, it will be created with the specified value.