$isArray - Amazon DocumentDB

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

$isArray

Amazon DocumentDB의 $isArray 연산자는 문서의 필드가 배열인지 확인하는 데 사용됩니다. 이 연산자는 집계 파이프라인 및 조건식에서 배열 유형 필드를 처리하는 데 유용할 수 있습니다.

파라미터

  • field: 배열인지 확인할 필드 경로입니다.

예제(MongoDB 쉘)

이 예제에서는 $isArray 연산자를 사용하여 "인벤토리" 필드가 배열인 문서를 식별하는 방법을 보여줍니다.

샘플 문서 생성

db.videos.insertMany([ { "_id":1, "name":"Live Soft", "inventory": {"Des Moines": 1000, "Ames" : 500}}, { "_id":2, "name":"Top Pilot", "inventory": {"Mason City": 250, "Des Moines": 1000}}, { "_id":3, "name":"Romancing the Rock", "inventory": {"Mason City": 250, "Ames" : 500}}, { "_id":4, "name":"Bravemind", "inventory": [{"location": "Mason City", "count": 250}, {"location": "Des Moines", "count": 1000}, {"location": "Ames", "count": 500}]} ]);

쿼리 예제

db.videos.aggregate([ { $match: { $isArray: "$inventory" } }, { $project: { _id: 1, name: 1, "inventory.location": 1, "inventory.count": 1 } } ]).pretty();

출력

{ "_id": 4, "name": "Bravemind", "inventory": [ { "location": "Mason City", "count": 250 }, { "location": "Des Moines", "count": 1000 }, { "location": "Ames", "count": 500 } ] }

코드 예제

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

Node.js
const { MongoClient } = require('mongodb'); async function run() { 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('videos'); const result = await collection.aggregate([ { $match: { $isArray: '$inventory' } }, { $project: { _id: 1, name: 1, "inventory.location": 1, "inventory.count": 1 } } ]).toArray(); console.log(result); await client.close(); } run();
Python
from pymongo import MongoClient 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['videos'] result = list(collection.aggregate([ { '$match': { '$isArray': '$inventory' } }, { '$project': { '_id': 1, 'name': 1, 'inventory.location': 1, 'inventory.count': 1 } } ])) print(result) client.close()