$ne - Amazon DocumentDB

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

$ne

$ne 集計演算子は 2 つの値を比較し、等しくtrueない場合は を返し、等しくない場合は を返しますfalse

パラメータ

  • expression1: 比較する最初の値。

  • expression2: 比較する 2 番目の値。

例 (MongoDB シェル)

次の例は、 $ne演算子を使用してステータスが変更された注文を識別する方法を示しています。

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

db.orders.insertMany([ { _id: 1, orderId: "A123", status: "shipped", expectedStatus: "shipped" }, { _id: 2, orderId: "B456", status: "pending", expectedStatus: "shipped" }, { _id: 3, orderId: "C789", status: "delivered", expectedStatus: "delivered" } ]);

クエリの例

db.orders.aggregate([ { $project: { orderId: 1, status: 1, expectedStatus: 1, needsAttention: { $ne: ["$status", "$expectedStatus"] } } } ]);

出力

[ { _id: 1, orderId: 'A123', status: 'shipped', expectedStatus: 'shipped', needsAttention: false }, { _id: 2, orderId: 'B456', status: 'pending', expectedStatus: 'shipped', needsAttention: true }, { _id: 3, orderId: 'C789', status: 'delivered', expectedStatus: 'delivered', needsAttention: false } ]

コードの例

$ne 集計演算子を使用するコード例を表示するには、使用する言語のタブを選択します。

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('orders'); const result = await collection.aggregate([ { $project: { orderId: 1, status: 1, expectedStatus: 1, needsAttention: { $ne: ["$status", "$expectedStatus"] } } } ]).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['orders'] result = list(collection.aggregate([ { '$project': { 'orderId': 1, 'status': 1, 'expectedStatus': 1, 'needsAttention': { '$ne': ['$status', '$expectedStatus'] } } } ])) print(result) client.close() example()