$all - Amazon DocumentDB

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

$all

Amazon DocumentDB の $all演算子は、フィールドの値が配列であり、配列内の要素の順序に関係なく、指定されたすべての要素を含むドキュメントを一致させるために使用されます。

パラメータ

  • field: チェックするフィールドの名前。

  • [value1, value2, ...]: 配列内で一致する値のリスト。

 

$all$elemMatch内での の使用

$all 式内での $elemMatch演算子の使用に関する制限$all 式内での $elemMatch の使用については、「」を参照してください。

 

フィールド名のドル ($)

ネストされたオブジェクトの $all$プレフィックス付きフィールドのクエリに関する制限フィールド名のドル ($) とドット (.)については、「」を参照してください。

例 (MongoDB シェル)

次の例は、「Colors」フィールドが「Red」と「Blue」の両方を含む配列であるドキュメントを取得するために $all演算子を使用する方法を示しています。

サンプルドキュメントを作成する

db.example.insertMany([ { "Item": "Pen", "Colors": ["Red", "Blue", "Green"] }, { "Item": "Notebook", "Colors": ["Blue", "White"] }, { "Item": "Poster Paint", "Colors": ["Red", "Yellow", "White"] } ])

クエリの例

db.example.find({ "Colors": { $all: ["Red", "Blue"] } }).pretty()

出力

{ "_id" : ObjectId("6137d6c5b3a1d35e0b6ee6ad"), "Item" : "Pen", "Colors" : [ "Red", "Blue", "Green" ] }

コードの例

$all コマンドを使用するためのコード例を表示するには、使用する言語のタブを選択します。

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('test'); const collection = db.collection('example'); const result = await collection.find({ "Colors": { $all: ["Red", "Blue"] } }).toArray(); console.log(result); await 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['test'] collection = db['example'] result = list(collection.find({ "Colors": { "$all": ["Red", "Blue"] } })) print(result) client.close() example()