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
Insert & Find
Insert documents and query a collection.
Aggregation Pipeline
Build multi-stage pipelines for analytics.
Indexes
Create single, compound, and text indexes for performance.
Update & Delete
Modify and remove documents with update operators.
Collections & Schema
Manage collections, validators, and capped collections.
Replica Set
Configure a replica set for high availability.