Mongodb Operators
MongoDB operators are special reserved keywords used to perform various operations in MongoDB queries. They are categorized into several groups based on their functionality. Here's a breakdown:
1. Comparison Operators
These are used to compare field values in queries.
Operator | Description | Example |
---|---|---|
$eq | Matches values equal to a specified value. | { age: { $eq: 25 } } |
$ne | Matches values not equal to a specified value. | { age: { $ne: 25 } } |
$gt | Matches values greater than a specified value. | { age: { $gt: 25 } } |
$gte | Matches values greater than or equal to a specified value. | { age: { $gte: 25 } } |
$lt | Matches values less than a specified value. | { age: { $lt: 25 } } |
$lte | Matches values less than or equal to a specified value. | { age: { $lte: 25 } } |
$in | Matches any value in a specified array. | { age: { $in: [25, 30, 35] } } |
$nin | Matches none of the values in a specified array. | { age: { $nin: [25, 30, 35] } } |
2. Logical Operators
These are used to combine multiple conditions.
Operator | Description | Example |
---|---|---|
$and | Joins queries with a logical AND. | { $and: [ { age: { $gt: 20 } }, { age: { $lt: 30 } } ] } |
$or | Joins queries with a logical OR. | { $or: [ { age: { $lt: 20 } }, { age: { $gt: 30 } } ] } |
$not | Inverts a query condition. | { age: { $not: { $gt: 25 } } } |
$nor | Matches documents that fail all the conditions. | { $nor: [ { age: { $lt: 20 } }, { age: { $gt: 30 } } ] } |
3. Element Operators
These query for fields based on the presence or absence of data.
Operator | Description | Example |
---|---|---|
$exists | Checks if a field exists or not. | { name: { $exists: true } } |
$type | Matches fields of a specific BSON type. | { age: { $type: "int" } } |
4. Array Operators
These are used to work with arrays.
Operator | Description | Example |
---|---|---|
$all | Matches arrays containing all specified elements. | { tags: { $all: ["sports", "music"] } } |
$elemMatch | Matches arrays containing at least one element that matches all specified conditions. | { scores: { $elemMatch: { $gte: 80, $lte: 90 } } } |
$size | Matches arrays with the specified number of elements. | { tags: { $size: 3 } } |
5. Evaluation Operators
These are used to apply functions or custom conditions.
Operator | Description | Example |
---|---|---|
$regex | Matches strings with a regular expression. | { name: { $regex: /^A/ } } |
$expr | Allows using aggregation expressions in queries. | { $expr: { $gt: ["$age", 30] } } |
$jsonSchema | Validates documents against a JSON schema. | { $jsonSchema: { properties: { age: { type: "number" } } } } |
6. Update Operators
These modify documents during an update operation.
Operator | Description | Example |
---|---|---|
$set | Sets the value of a field. | { $set: { name: "John" } } |
$unset | Removes a field. | { $unset: { name: "" } } |
$inc | Increments a field by a specified value. | { $inc: { age: 1 } } |
$rename | Renames a field. | { $rename: { oldName: "newName" } } |
$push | Adds an element to an array. | { $push: { tags: "newTag" } } |
$pull | Removes elements from an array. | { $pull: { tags: "oldTag" } } |
$addToSet | Adds a value to an array if it doesn’t exist. | { $addToSet: { tags: "uniqueTag" } } |
7. Aggregation Operators
These are used in aggregation pipelines.
Operator | Description | Example |
---|---|---|
$match | Filters documents to pass only those that match the condition. | { $match: { age: { $gte: 30 } } } |
$group | Groups documents by a specified key and performs aggregations. | { $group: { _id: "$city", total: { $sum: 1 } } } |
$project | Shapes the documents to return specific fields. | { $project: { name: 1, age: 1 } } |
$sort | Sorts the documents. | { $sort: { age: -1 } } |
$lookup | Performs a join with another collection. | { $lookup: { from: "orders", localField: "userId", foreignField: "_id", as: "userOrders" } } |
This is a broad overview of MongoDB operators. Let me know if you'd like more details or examples for any specific operator!