$[] - Amazon DocumentDB

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

$[]

$[] 모든 위치 연산자는 배열의 모든 요소를 업데이트합니다. 배열 필드의 모든 요소를 수정해야 할 때 사용됩니다.

파라미터

  • field.$[]: 모든 요소를 업데이트하기 위한 모든 위치 연산자가 있는 배열 필드입니다.

예제(MongoDB 쉘)

다음 예제에서는 $[] 연산자를 사용하여 모든 배열 요소를 업데이트하는 방법을 보여줍니다.

샘플 문서 생성

db.products.insertOne({ _id: 1, name: "Laptop", prices: [1000, 1100, 1200] });

쿼리 예제

db.products.updateOne( { _id: 1 }, { $inc: { "prices.$[]": 50 } } );

출력

{ "_id" : 1, "name" : "Laptop", "prices" : [ 1050, 1150, 1250 ] }

코드 예제

$[] 연산자 사용에 대한 코드 예제를 보려면 사용하려는 언어의 탭을 선택합니다.

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('products'); await collection.updateOne( { _id: 1 }, { $inc: { "prices.$[]": 50 } } ); const updatedDocument = await collection.findOne({ _id: 1 }); 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.products collection.update_one( {'_id': 1}, {'$inc': {'prices.$[]': 50}} ) updated_document = collection.find_one({'_id': 1}) print(updated_document) client.close() update_document()