MongoDB Query Operators for Node.js Developers
MongoDB has many query operators. Here are the ones Node.js developers use most.
MongoDB Query Operators for Node.js Developers
MongoDB has many query operators. Here are the ones Node.js developers use most.
Comparison Operators
- $eq: equal (default, usually omitted).
- $ne: not equal.
- $gt, $gte: greater than, greater than or equal.
- $lt, $lte: less than, less than or equal.
- $in: value is in an array.
- $nin: value is not in an array.
- $exists: field exists or not.
User.find({ age: { $gte: 18, $lte: 35 } });
User.find({ role: { $in: ['admin', 'moderator'] } });
User.find({ deletedAt: { $exists: false } });
Logical Operators
- $and: all conditions match.
- $or: any condition matches.
- $nor: no condition matches.
- $not: negates a condition.
User.find({ $or: [{ age: { $lt: 18 } }, { age: { $gt: 65 } }] });
Element Operators
- $exists: field exists or not.
- $type: field is of a specific BSON type.
User.find({ email: { $exists: true } });
Array Operators
- $all: array contains all of the given values.
- $elemMatch: array contains at least one element matching all conditions.
- $size: array has a specific size.
User.find({ skills: { $all: ['node', 'react'] } });
User.find({ posts: { $elemMatch: { status: 'published', likes: { $gt: 100 } } } });
Evaluation Operators
- $regex: matches a regex.
- $text: full-text search (requires a text index).
- $where: runs JavaScript (avoid; slow).
User.find({ email: { $regex: /@gmail.com$/ } });
User.find({ $text: { $search: 'nodejs developer' } });
Common Patterns
Find by Multiple Fields
User.find({ role: 'admin', age: { $gte: 18 } });
Find by Date Range
User.find({ createdAt: { $gte: new Date('2025-01-01'), $lt: new Date('2025-02-01') } });
Find by ObjectId
Post.find({ authorId: new mongoose.Types.ObjectId(userId) });
Find Where Array Contains a Value
User.find({ skills: 'node' });
Find Where Array Contains Multiple Values
User.find({ skills: { $all: ['node', 'react'] } });
The Takeaway
Common MongoDB query operators: 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 them to build queries in Mongoose without writing raw SQL-like statements.
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'] } }).
Create a text index: schema.index({ title: 'text', body: 'text' }). Then query: Post.find({ $text: { $search: 'nodejs developer' } }). Add a text index only on fields you search; it is heavy.
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.
Ready to master Node.js completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master Node.js
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

