$concatArrays - Amazon DocumentDB

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

$concatArrays

Amazon DocumentDB의 $concatArrays 집계 연산자는 두 개 이상의 배열을 단일 배열로 연결하는 데 사용됩니다. 이는 추가 처리 또는 분석을 위해 여러 데이터 배열을 단일 배열로 결합해야 할 때 유용할 수 있습니다.

파라미터

  • array1: 연결할 첫 번째 배열입니다.

  • array2: 연결할 두 번째 배열입니다.

  • [array3, ...]: (선택 사항) 연결할 추가 배열입니다.

예제(MongoDB 쉘)

다음 예제에서는 $concatArrays 연산자를 사용하여 두 배열을 단일 배열로 결합하는 방법을 보여줍니다.

샘플 문서 생성

db.collection.insertMany([ { "_id": 1, "name": "John Doe", "hobbies": ["reading", "swimming"], "skills": ["programming", "design"] }, { "_id": 2, "name": "Jane Smith", "hobbies": ["hiking", "cooking"], "skills": ["marketing", "analysis"] } ]);

쿼리 예제

db.collection.aggregate([ { $project: { _id: 0, name: 1, all_activities: { $concatArrays: ["$hobbies", "$skills"] } } } ]);

출력

[ { "name": "John Doe", "all_activities": [ "reading", "swimming", "programming", "design" ] }, { "name": "Jane Smith", "all_activities": [ "hiking", "cooking", "marketing", "analysis" ] } ]

코드 예제

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

Node.js
const { MongoClient } = require('mongodb'); async function example() { 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('mydb'); const collection = db.collection('mycollection'); const result = await collection.aggregate([ { $project: { _id: 0, name: 1, all_activities: { $concatArrays: ['$hobbies', '$skills'] } } } ]).toArray(); console.log(result); client.close(); } example();
Python
from pymongo import MongoClient def example(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client['mydb'] collection = db['mycollection'] result = list(collection.aggregate([ { '$project': { '_id': 0, 'name': 1, 'all_activities': { '$concatArrays': ['$hobbies', '$skills'] } } } ])) print(result) client.close() example()