$ne
The $ne aggregation operator compares two values and returns true if they are not equal, otherwise returns false.
Parameters
-
expression1: The first value to compare. -
expression2: The second value to compare.
Example (MongoDB Shell)
The following example demonstrates using the $ne operator to identify orders with status changes.
Create sample documents
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" } ]);
Query example
db.orders.aggregate([ { $project: { orderId: 1, status: 1, expectedStatus: 1, needsAttention: { $ne: ["$status", "$expectedStatus"] } } } ]);
Output
[
{ _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 }
]
Code examples
To view a code example for using the $ne aggregation operator, choose the tab for the language that you want to use: