MongoDB — Element & Evaluation Operators
Query documents by field existence ($exists), BSON data type ($type), regex pattern ($regex), and document field evaluation ($expr).
1Element Operators ($exists & $type)
$exists & $type Examples
// Find users who have a phone field present
db.users.find({ phone: { $exists: true } });
// Find users where age is stored as a String instead of Number
db.users.find({ age: { $type: "string" } });
2Evaluation Operators ($regex & $expr)
$regex & $expr Examples
// Case-insensitive regex search for users named "balaji"
db.users.find({ name: { $regex: /^balaji/i } });
// $expr: Compare two fields inside the SAME document!
// Find orders where spent amount exceeds total budget
db.orders.find({
$expr: { $gt: ["$spentAmount", "$budgetLimit"] }
});