翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
$in
Amazon DocumentDB の $in演算子は、フィールドの値が配列で指定された値のいずれかと等しいドキュメントを検索できる論理クエリ演算子です。
パラメータ
-
field: 指定された配列と照合するフィールド。
-
[value1, value2, ...]: 指定されたフィールドに一致する値の配列。
フィールド名のドル ($)
ネストされたオブジェクトの $inの$プレフィックス付きフィールドのクエリに関する制限フィールド名のドル ($) とドット (.)については、「」を参照してください。
例 (MongoDB シェル)
次の例は、 $in演算子を使用して、 colorフィールドが指定された配列の値の 1 つであるドキュメントを検索する方法を示しています。
サンプルドキュメントを作成する
db.colors.insertMany([
{ "_id": 1, "color": "red" },
{ "_id": 2, "color": "green" },
{ "_id": 3, "color": "blue" },
{ "_id": 4, "color": "yellow" },
{ "_id": 5, "color": "purple" }
])
クエリの例
db.colors.find({ "color": { "$in": ["red", "blue", "purple"] } })
出力
{ "_id": 1, "color": "red" },
{ "_id": 3, "color": "blue" },
{ "_id": 5, "color": "purple" }
コードの例
$in コマンドを使用するコード例を表示するには、使用する言語のタブを選択します。
- Node.js
-
const { MongoClient } = require('mongodb');
async function findByIn() {
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('colors');
const result = await collection.find({ "color": { "$in": ["red", "blue", "purple"] } }).toArray();
console.log(result);
await client.close();
}
findByIn();
- Python
-
from pymongo import MongoClient
def find_by_in():
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.colors
result = list(collection.find({ "color": { "$in": ["red", "blue", "purple"] } }))
print(result)
client.close()
find_by_in()