Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions,
see Getting Started with Amazon Web Services in China
(PDF).
$eq
The $eq aggregation operator compares two values and returns true if they are equal, otherwise returns false.
Parameters
Example (MongoDB Shell)
The following example demonstrates using the $eq operator to check if product quantities match target values.
Create sample documents
db.inventory.insertMany([
{ _id: 1, item: "Widget", qty: 50, target: 50 },
{ _id: 2, item: "Gadget", qty: 30, target: 50 },
{ _id: 3, item: "Tool", qty: 50, target: 40 }
]);
Query example
db.inventory.aggregate([
{
$project: {
item: 1,
qty: 1,
target: 1,
meetsTarget: { $eq: ["$qty", "$target"] }
}
}
]);
Output
[
{ _id: 1, item: 'Widget', qty: 50, target: 50, meetsTarget: true },
{ _id: 2, item: 'Gadget', qty: 30, target: 50, meetsTarget: false },
{ _id: 3, item: 'Tool', qty: 50, target: 40, meetsTarget: false }
]
Code examples
To view a code example for using the $eq aggregation operator, choose the tab for the language that you want to use:
- 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.aggregate([
{
$project: {
item: 1,
qty: 1,
target: 1,
meetsTarget: { $eq: ["$qty", "$target"] }
}
}
]).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.aggregate([
{
'$project': {
'item': 1,
'qty': 1,
'target': 1,
'meetsTarget': { '$eq': ['$qty', '$target'] }
}
}
]))
print(result)
client.close()
example()