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