Skip to content
MongoDB

Query Operators

Use comparison, logical, array, and regex operators.

By EZ4Code Team
queryoperatorsfilter

Code

// Comparison operators
db.products.find({ price: { $gt: 100, $lte: 500 } });
db.products.find({ status: { $in: ["active", "pending"] } });

// Logical operators
db.products.find({
  $and: [
    { price: { $gt: 100 } },
    { $or: [{ category: "books" }, { category: "music" }] }
  ]
});

// Array and element operators
db.posts.find({ tags: { $all: ["mongo", "db"] } });
db.posts.find({ "tags.0": "mongo" });          // first element
db.posts.find({ featured: { $exists: true } });

// Regex
db.users.find({ name: /^A/i });

Explanation

MongoDB query operators compose into rich filters: $gt/$lte compare, $in matches any value, and $and/$or combine predicates. Array operators like $all and the indexed position syntax match array contents. $exists tests field presence and regex matches string patterns.

More MongoDB Snippets