Reputation: 1648
When I run the following Mongoose aggregate query, I get an error:
Query:
await Users.aggregate([
{ $eq: ["$bot", false] }
]);
Error:
MongoError: Unrecognized pipeline stage name: '$eq'
at queryCallback (/root/Geola/node_modules/mongodb-core/lib/cursor.js:248:25)
at /root/Geola/node_modules/mongodb-core/lib/connection/pool.js:532:18
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickDomainCallback (internal/process/next_tick.js:218:9)
Am I using it wrong?
MongoDB: 4.0.6
Mongoose: 5.4.13
Ubuntu: 16.04
Upvotes: 1
Views: 1734
Reputation: 719
The $eq
operator matches documents where the value of a field equals the specified value.
{ <field>: { $eq: <value> } }
You can try this method to filter based on a condition
db.inventory.find( { bot: { $eq: false } } )
More details in docs
When you use Aggregate. Documents enter a multi-stage pipeline that transforms the documents into aggregated results. You have to use match to pass the match pipeline in your case.
You can learn more about aggregate pipeline here
Upvotes: 1
Reputation: 1648
Not sure why my original method didn't work, or why this works any better, but here's what fixed it for me:
await Users.aggregate([
{ $match: { $expr: { $eq: ["$bot", false] } } }
]);
Upvotes: 1