It runs JavaScript in the database for each document. Slow and a security risk. There is almost always a better way to write the query with other operators. Avoid $where.
MongoDB Query Operators for Node.js Developers
Why avoid $where in MongoDB?
The answer
More FAQs in MongoDB Query Operators for Node.js Developers
Comparison ($eq, $ne, $gt, $gte, $lt, $lte, $in, $nin, $exists), logical ($and, $or, $nor, $not), element ($exists, $type), array ($all, $elemMatch, $size), and evaluation ($regex, $text, $where).
Use $gte and $lte with Date objects: User.find({ createdAt: { $gte: new Date('2025-01-01'), $lt: new Date('2025-02-01') } }). Always use Date objects, not strings.
User.find({ skills: 'node' }). For multiple values, use $all: User.find({ skills: { $all: ['node', 'react'] } }). For at least one of multiple, use $in: User.find({ skills: { $in: ['node', 'react'] } }).
Still have questions?
Browse all our FAQs or reach out to our support team
Want to upskill yourself?
Your learning library
Small steps. Big possibilities.
Find your next course. Build something great.