$expr - Amazon DocumentDB

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

$expr

バージョン 4.0 の新機能。

Elastic クラスターではサポートされていません。

Amazon DocumentDB の $expr演算子を使用すると、クエリ言語内で集計式を使用できます。これにより、集約パイプラインステージを使用する方法と同様に、ドキュメント内のフィールドに対して複雑な比較と計算を実行できます。

パラメータ

  • expression: ブール値を返す式。ドキュメントフィールドで比較と計算を実行できます。

例 (MongoDB シェル)

次の例は、 $expr演算子を使用して、 manufacturingCostフィールドが priceフィールドより大きいすべてのドキュメントを検索する方法を示しています。

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

db.inventory.insertMany([ { item: "abc", manufacturingCost: 500, price: 100 }, { item: "def", manufacturingCost: 300, price: 450 }, { item: "ghi", manufacturingCost: 400, price: 120 } ]);

クエリの例

db.inventory.find({ $expr: { $gt: ["$manufacturingCost", "$price"] } })

出力

{ "_id" : ObjectId("60b9d4d68d2cac581bc5a89a"), "item" : "abc", "manufacturingCost" : 500, "price" : 100 }, { "_id" : ObjectId("60b9d4d68d2cac581bc5a89c"), "item" : "ghi", "manufacturingCost" : 400, "price" : 120 }

コードの例

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

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('inventory'); const result = await collection.find({ $expr: { $gt: ['$manufacturingCost', '$price'] } }).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['inventory'] result = list(collection.find({ '$expr': { '$gt': ['$manufacturingCost', '$price'] } })) print(result) client.close() example()