$set - Amazon DocumentDB

$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:

Node.js
const { MongoClient } = require('mongodb'); async function updateDocument() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); const collection = db.collection('example'); await collection.updateOne( { "Item": "Pen" }, { $set: { "Item": "Gel Pen" } } ); const updatedDocument = await collection.findOne({ "Item": "Gel Pen" }); console.log(updatedDocument); await client.close(); } updateDocument();
Python
from pymongo import MongoClient def update_document(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client.test collection = db.example collection.update_one( {"Item": "Pen"}, {"$set": {"Item": "Gel Pen"}} ) updated_document = collection.find_one({"Item": "Gel Pen"}) print(updated_document) client.close() update_document()