$set - Amazon DocumentDB

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

$set

Amazon DocumentDB の $set演算子は、ドキュメント内の指定されたフィールドの値を更新するために使用されます。この演算子を使用すると、新しいフィールドを追加したり、ドキュメント内の既存のフィールドを変更したりできます。これは、Amazon DocumentDB と互換性のある MongoDB Java ドライバーの基本的な更新演算子です。

パラメータ

  • field: 更新するフィールド。

  • value: フィールドの新しい値。

例 (MongoDB シェル)

次の例は、 $set演算子を使用してドキュメントの Itemフィールドを更新する方法を示しています。

サンプルドキュメントを作成する

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 } } ])

クエリの例

db.example.update( { "Item": "Pen" }, { $set: { "Item": "Gel Pen" } } )

出力

{ "Item": "Gel Pen", "Colors": ["Red", "Green", "Blue", "Black"], "Inventory": { "OnHand": 244, "MinOnHand": 72 } }

コードの例

$set コマンドを使用するためのコード例を表示するには、使用する言語のタブを選択します。

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()