MongoDB operators are used in queries to perform a variety of operations such as comparison, logical operations, updating documents, and more. Here’s a breakdown of the most commonly used MongoDB operators:
### **1. Comparison Operators**
These operators are used to compare values within documents.
- **`$eq`**: Matches values that are equal to a specified value.
```javascript
{ age: { $eq: 25 } }
```
*Equivalent to SQL: `WHERE age = 25`*
- **`$ne`**: Matches values that are not equal to a specified value.
```javascript
{ age: { $ne: 25 } }
```
*Equivalent to SQL: `WHERE age != 25`*
- **`$gt`**: Matches values that are greater than a specified value.
```javascript
{ age: { $gt: 25 } }
```
*Equivalent to SQL: `WHERE age > 25`*
- **`$gte`**: Matches values that are greater than or equal to a specified value.
```javascript
{ age: { $gte: 25 } }
```
*Equivalent to SQL: `WHERE age >= 25`*
- **`$lt`**: Matches values that are less than a specified value.
```javascript
{ age: { $lt: 25 } }
```
*Equivalent to SQL: `WHERE age < 25`*
- **`$lte`**: Matches values that are less than or equal to a specified value.
```javascript
{ age: { $lte: 25 } }
```
*Equivalent to SQL: `WHERE age <= 25`*
- **`$in`**: Matches any of the values specified in an array.
```javascript
{ age: { $in: [20, 25, 30] } }
```
*Equivalent to SQL: `WHERE age IN (20, 25, 30)`*
- **`$nin`**: Matches none of the values specified in an array.
```javascript
{ age: { $nin: [20, 25, 30] } }
```
*Equivalent to SQL: `WHERE age NOT IN (20, 25, 30)`*
### **2. Logical Operators**
These operators combine multiple conditions.
- **`$and`**: Joins query clauses with a logical AND returns documents that match all the conditions.
```javascript
{ $and: [ { age: { $gt: 25 } }, { city: "New York" } ] }
```
*Equivalent to SQL: `WHERE age > 25 AND city = 'New York'`*
- **`$or`**: Joins query clauses with a logical OR returns documents that match any of the conditions.
```javascript
{ $or: [ { age: { $lt: 25 } }, { city: "Los Angeles" } ] }
```
*Equivalent to SQL: `WHERE age < 25 OR city = 'Los Angeles'`*
- **`$not`**: Inverts the effect of a query expression.
```javascript
{ age: { $not: { $gte: 25 } } }
```
*Equivalent to SQL: `WHERE NOT age >= 25`*
- **`$nor`**: Joins query clauses with a logical NOR returns documents that fail all the conditions.
```javascript
{ $nor: [ { age: { $gte: 25 } }, { city: "Los Angeles" } ] }
```
*Equivalent to SQL: `WHERE NOT (age >= 25 OR city = 'Los Angeles')`*
### **3. Element Operators**
These operators are used to query documents based on the existence of fields or specific data types.
- **`$exists`**: Matches documents that have the specified field.
```javascript
{ middle_name: { $exists: true } }
```
*Equivalent to SQL: `WHERE middle_name IS NOT NULL`*
- **`$type`**: Selects documents if a field is of the specified type.
```javascript
{ age: { $type: "int" } }
```
*Equivalent to SQL: Filtering by data type.*
### **4. Array Operators**
These operators are used to query and update array fields.
- **`$all`**: Matches arrays that contain all elements specified in the query.
```javascript
{ tags: { $all: ["red", "blue"] } }
```
*Equivalent to SQL: `WHERE tags CONTAINS ALL ('red', 'blue')`*
- **`$elemMatch`**: Matches documents that contain an array field with at least one element that matches all the specified query criteria.
```javascript
{ results: { $elemMatch: { score: { $gt: 80 }, grade: "A" } } }
```
*Equivalent to SQL: Filtering an array with multiple conditions.*
- **`$size`**: Selects documents if the array field is a specified size.
```javascript
{ tags: { $size: 3 } }
```
*Equivalent to SQL: `WHERE ARRAY_LENGTH(tags) = 3`*
### **5. Update Operators**
These operators are used to modify documents.
- **`$set`**: Sets the value of a field in a document.
```javascript
{ $set: { age: 30 } }
```
*Equivalent to SQL: `UPDATE SET age = 30`*
- **`$unset`**: Removes the specified field from a document.
```javascript
{ $unset: { middle_name: "" } }
```
*Equivalent to SQL: `ALTER TABLE DROP COLUMN middle_name`*
- **`$inc`**: Increments the value of the field by the specified amount.
```javascript
{ $inc: { age: 1 } }
```
*Equivalent to SQL: `UPDATE SET age = age + 1`*
- **`$push`**: Adds an element to an array.
```javascript
{ $push: { tags: "green" } }
```
*Equivalent to SQL: `UPDATE tags SET tags = array_append(tags, 'green')`*
- **`$pull`**: Removes all array elements that match a specified query.
```javascript
{ $pull: { tags: "red" } }
```
*Equivalent to SQL: `UPDATE tags SET tags = array_remove(tags, 'red')`*
### **6. Aggregation Operators**
These are used in the aggregation pipeline to process data and return computed results.
- **`$sum`**: Sums up the values.
```javascript
{ $group: { _id: null, total: { $sum: "$quantity" } } }
```
*Equivalent to SQL: `SUM(quantity)`*
- **`$avg`**: Computes the average of the numeric values.
```javascript
{ $group: { _id: null, average: { $avg: "$quantity" } } }
```
*Equivalent to SQL: `AVG(quantity)`*
- **`$max`**: Returns the maximum value.
```javascript
{ $group: { _id: null, maxQuantity: { $max: "$quantity" } } }
```
*Equivalent to SQL: `MAX(quantity)`*
- **`$min`**: Returns the minimum value.
```javascript
{ $group: { _id: null, minQuantity: { $min: "$quantity" } } }
```
*Equivalent to SQL: `MIN(quantity)`*
- **`$first`**: Returns the first document from the group.
```javascript
{ $group: { _id: null, firstQuantity: { $first: "$quantity" } } }
```
*Equivalent to SQL: `FIRST(quantity)`*
- **`$last`**: Returns the last document from the group.
```javascript
{ $group: { _id: null, lastQuantity: { $last: "$quantity" } } }
```
*Equivalent to SQL: `LAST(quantity)`*
# Conclusion
MongoDB operators provide a wide range of tools for querying and manipulating documents in a database. They are powerful and flexible, allowing you to perform complex operations that would be cumbersome in traditional SQL-based systems. Understanding and using these operators effectively is key to mastering MongoDB.
Comments
Post a Comment