$replaceWith - Amazon DocumentDB

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

$replaceWith

버전 8.0의 새로운 기능

Elastic 클러스터에서는 지원되지 않습니다.

Amazon DocumentDB의 $replaceWith 집계 단계는 입력 문서를 새 문서로 대체하는 데 사용됩니다. _id 필드를 포함하여 입력 문서의 모든 기존 필드는 새 문서로 대체됩니다. $replaceWith는 일반적으로 문서를 평면화하거나 임베디드 문서를 최상위 수준으로 승격하는 데 사용됩니다.

파라미터

  • <replacement> (필수): 기존 문서를 대체할 새 문서입니다.

예제(MongoDB 쉘)

다음 예제에서는 $replaceWith 연산자를 사용하여 Amazon DocumentDB 컬렉션의 기존 문서를 교체하는 방법을 보여줍니다.

샘플 문서 생성

db.restaurants.insertMany([ { "restaurantId": "REST-0Y9GL0", "name": "Biryani Adda", "cuisine": "Indian", "ratings": [ 3, 4, 3, 2, 2, 4, 1, 5, 5, 5 ] }, { "restaurantId": "REST-8L2PX9", "name": "The Burger Spot", "cuisine": "American", "ratings": [ 2, 3, 4, 5, 3, 1, 1, 2, 4 ] } ]);

쿼리 예제

db.restaurants.aggregate([ { $replaceWith: { name: "$name", cuisine: "$cuisine", rating: { $avg: "$ratings" } } } ]);

출력

[ { name: 'Biryani Adda', cuisine: 'Indian', rating: 3.4 }, { name: 'The Burger Spot', cuisine: 'American', rating: 2.7777777777777777 } ]

코드 예제

$replaceWith 명령을 사용하기 위한 코드 예제를 보려면 사용하려는 언어의 탭을 선택합니다.

Node.js
const { MongoClient } = require('mongodb'); async function replaceDoc() { 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('restaurants'); const result = await collection.aggregate([ { $replaceWith: { name: "description_index", cuisine: 2, rating: { $avg: "$ratings" } } } ]).toArray(); console.log(result); client.close(); } replaceDoc();
Python
from pymongo import MongoClient def replace_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.restaurants result = list(collection.aggregate([ { '$replaceWith': { 'name': "$name", 'cuisine': "$cuisine", 'rating': { '$avg': "$ratings"} } } ])) print(result) client.close() replace_document()