$ne - Amazon DocumentDB

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

$ne

$ne 집계 연산자는 두 값을 비교하고 값이 같true지 않으면를 반환하고 그렇지 않으면를 반환합니다false.

파라미터

  • expression1: 비교할 첫 번째 값입니다.

  • expression2: 비교할 두 번째 값입니다.

예제(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()