$set
The $set operator in Amazon DocumentDB is used to update the value of a specified field in a document. This operator allows you to add new fields or modify existing ones within a document. It is a fundamental update operator in the MongoDB Java driver, which is compatible with Amazon DocumentDB.
Parameters
-
field: The field to update. -
value: The new value for the field.
Example (MongoDB Shell)
The following example demonstrates how to use the $set operator to update the Item field in a document.
Create sample documents
db.example.insert([ { "Item": "Pen", "Colors": ["Red", "Green", "Blue", "Black"], "Inventory": { "OnHand": 244, "MinOnHand": 72 } }, { "Item": "Poster Paint", "Colors": ["Red", "Green", "Blue", "White"], "Inventory": { "OnHand": 120, "MinOnHand": 36 } } ])
Query example
db.example.update( { "Item": "Pen" }, { $set: { "Item": "Gel Pen" } } )
Output
{
"Item": "Gel Pen",
"Colors": ["Red", "Green", "Blue", "Black"],
"Inventory": {
"OnHand": 244,
"MinOnHand": 72
}
}
Code examples
To view a code example for using the $set command, choose the tab for the language that you want to use: