MongoDb Operators
MongoDB operators in Node.js can be categorized into various groups like comparison, logical, array, update, element, and more. Below is a comprehensive list of commonly used MongoDB operators, along with their explanations, examples, and expected outputs:
Comparison Operators
Operator | Description | Example | Expected Output |
$eq | Matches values equal to a specified value. | { age: { $eq: 25 } } — Finds all documents where age is 25 . | [ { _id: 1, age: 25 } ] |
$ne | Matches values not equal to a specified value. | { age: { $ne: 25 } } — Finds all documents where age is not 25 . | [ { _id: 2, age: 30 } ] |
$gt | Matches values greater than a specified value. | { age: { $gt: 20 } } — Finds all documents where age is greater than 20 . | [ { _id: 2, age: 30 } ] |
$gte | Matches values greater than or equal to a specified value. | { age: { $gte: 25 } } — Finds all documents where age is >= 25 . | [ { _id: 1, age: 25 }, { _id: 2, age: 30 } ] |
$lt | Matches values less than a specified value. | { age: { $lt: 30 } } — Finds all documents where age is < 30 . | [ { _id: 1, age: 25 } ] |
$lte | Matches values less than or equal to a specified value. | { age: { $lte: 25 } } — Finds all documents where age is <= 25 . | [ { _id: 1, age: 25 } ] |
$in | Matches any value in an array. | { age: { $in: [25, 30] } } — Finds all documents where age is 25 or 30 . | [ { _id: 1, age: 25 }, { _id: 2, age: 30 } ] |
$nin | Matches none of the values in an array. | { age: { $nin: [25, 30] } } — Finds all documents where age is not 25 or 30 . | [ { _id: 3, age: 40 } ] |
Logical Operators
Operator | Description | Example | Expected Output |
$and | Joins query clauses with a logical AND. | { $and: [{ age: { $gte: 25 } }, { age: { $lt: 35 } }] } — Finds all documents where 25 <= age < 35 . | [ { _id: 1, age: 25 }, { _id: 2, age: 30 } ] |
$or | Joins query clauses with a logical OR. | { $or: [{ age: { $lt: 20 } }, { age: { $gt: 30 } }] } — Finds all documents where age < 20 or age > 30 . | [ { _id: 3, age: 40 } ] |
$not | Inverts the effect of a query clause. | { age: { $not: { $gte: 30 } } } — Finds all documents where age is not >= 30 . | [ { _id: 1, age: 25 } ] |
$nor | Joins query clauses with a logical NOR. | { $nor: [{ age: { $lt: 20 } }, { age: { $gte: 30 } }] } — Finds all documents where age is not < 20 or >= 30 . | [ { _id: 1, age: 25 } ] |
Element Operators
Operator | Description | Example | Expected Output |
$exists | Matches documents that have the specified field. | { field: { $exists: true } } — Finds all documents where field exists. | [ { _id: 1, field: 'value' } ] |
$type | Matches documents with a specific field type. | { field: { $type: 'string' } } — Finds all documents where field is a string. | [ { _id: 1, field: 'value' } ] |
Array Operators
Operator | Description | Example | Expected Output |
$all | Matches arrays containing all specified elements. | { tags: { $all: ['tech', 'blog'] } } — Finds all documents where tags contains tech and blog . | [ { _id: 1, tags: ['tech', 'blog', 'news'] } ] |
$size | Matches arrays with the specified size. | { tags: { $size: 3 } } — Finds all documents where tags contains exactly 3 elements. | [ { _id: 1, tags: ['tech', 'blog', 'news'] } ] |
$elemMatch | Matches arrays with at least one element matching a specified query. | { tags: { $elemMatch: { $eq: 'tech' } } } — Finds all documents where tags has an element equal to tech . | [ { _id: 1, tags: ['tech', 'blog'] } ] |
Update Operators
Operator | Description | Example | Expected Output |
$set | Sets the value of a field. | { $set: { name: 'John' } } — Updates name to 'John' . | [ { _id: 1, name: 'John' } ] |
$unset | Removes a field from the document. | { $unset: { name: '' } } — Removes the name field. | [ { _id: 1 } ] |
$inc | Increments the value of a field. | { $inc: { count: 1 } } — Increments count by 1 . | [ { _id: 1, count: 6 } ] |
$push | Adds an element to an array. | { $push: { tags: 'new' } } — Adds 'new' to the tags array. | [ { _id: 1, tags: ['tech', 'blog', 'new'] } ] |
$pull | Removes an element from an array. | { $pull: { tags: 'blog' } } — Removes 'blog' from the tags array. | [ { _id: 1, tags: ['tech'] } ] |
These are some key operators frequently used in Node.js applications with MongoDB. Let me know if you'd like to explore more examples or specific scenarios!
Below is a detailed explanation of all MongoDB operators, categorized by their purpose. Each section provides examples, scenarios where you would use the operator, and the expected output to give you a comprehensive understanding.
1. Comparison Operators
These are used to compare field values in documents.
$eq
- Equal
Matches values equal to a specified value.
Example:
db.collection.find({ age: { $eq: 25 } });
Input Data:
[
{ "_id": 1, "age": 25 },
{ "_id": 2, "age": 30 }
]
Output:
[ { "_id": 1, "age": 25 } ]
Use Case: Finding users of a specific age.
$ne
- Not Equal
Matches values not equal to a specified value.
Example:
db.collection.find({ age: { $ne: 25 } });
Output:
[ { "_id": 2, "age": 30 } ]
Use Case: Excluding certain values from a query result.
$gt
- Greater Than
Matches values greater than a specified value.
Example:
db.collection.find({ age: { $gt: 25 } });
Output:
[ { "_id": 2, "age": 30 } ]
Use Case: Finding records with numerical values above a threshold.
$gte
- Greater Than or Equal
Matches values greater than or equal to a specified value.
Example:
db.collection.find({ age: { $gte: 25 } });
Output:
[ { "_id": 1, "age": 25 }, { "_id": 2, "age": 30 } ]
$lt
- Less Than
Matches values less than a specified value.
Example:
db.collection.find({ age: { $lt: 30 } });
Output:
[ { "_id": 1, "age": 25 } ]
$lte
- Less Than or Equal
Matches values less than or equal to a specified value.
Example:
db.collection.find({ age: { $lte: 25 } });
Output:
[ { "_id": 1, "age": 25 } ]
$in
- Matches Any in a List
Matches documents where the value is in the provided array.
Example:
db.collection.find({ age: { $in: [25, 30] } });
Output:
[ { "_id": 1, "age": 25 }, { "_id": 2, "age": 30 } ]
Use Case: Querying for multiple possible values.
$nin
- Matches None in a List
Matches documents where the value is not in the provided array.
Example:
db.collection.find({ age: { $nin: [25, 30] } });
Output:
[ { "_id": 3, "age": 40 } ]
2. Logical Operators
These are used to combine multiple conditions in a query.
$and
- Logical AND
Matches documents that satisfy all the specified conditions.
Example:
db.collection.find({ $and: [{ age: { $gte: 25 } }, { age: { $lt: 30 } }] });
Output:
[ { "_id": 1, "age": 25 } ]
$or
- Logical OR
Matches documents that satisfy at least one condition.
Example:
db.collection.find({ $or: [{ age: { $lt: 20 } }, { age: { $gt: 30 } }] });
Output:
[ { "_id": 3, "age": 40 } ]
$not
- Logical NOT
Inverts the query condition.
Example:
db.collection.find({ age: { $not: { $gte: 30 } } });
Output:
[ { "_id": 1, "age": 25 } ]
$nor
- Logical NOR
Matches documents that fail to match all conditions.
Example:
db.collection.find({ $nor: [{ age: { $lt: 20 } }, { age: { $gte: 30 } }] });
Output:
[ { "_id": 1, "age": 25 } ]
3. Update Operators
These are used for modifying documents.
$set
- Sets the Value of a Field
Example:
db.collection.updateOne({ _id: 1 }, { $set: { name: "John" } });
Input:
{ "_id": 1, "name": "Doe" }
Output:
{ "_id": 1, "name": "John" }
$unset
- Removes a Field
Example:
db.collection.updateOne({ _id: 1 }, { $unset: { name: "" } });
Output:
{ "_id": 1 }
$inc
- Increment a Numeric Field
Example:
db.collection.updateOne({ _id: 1 }, { $inc: { count: 1 } });
Input:
{ "_id": 1, "count": 5 }
Output:
{ "_id": 1, "count": 6 }
$push
- Add to an Array
Example:
db.collection.updateOne({ _id: 1 }, { $push: { tags: "new" } });
Output:
{ "_id": 1, "tags": ["tech", "new"] }
$pull
- Remove from an Array
Example:
db.collection.updateOne({ _id: 1 }, { $pull: { tags: "tech" } });
Output:
{ "_id": 1, "tags": [] }
4. Array Operators
$all
- Matches Arrays with All Specified Elements
Example:
db.collection.find({ tags: { $all: ["tech", "blog"] } });
Output:
[ { "_id": 1, "tags": ["tech", "blog"] } ]
$size
- Matches Arrays of a Specific Size
Example:
db.collection.find({ tags: { $size: 2 } });
Output:
[ { "_id": 1, "tags": ["tech", "blog"] } ]
$elemMatch
- Matches a Single Element in an Array
Example:
db.collection.find({ scores: { $elemMatch: { $gte: 80, $lt: 90 } } });
Input:
{ "_id": 1, "scores": [50, 85, 95] }
Output:
[ { "_id": 1, "scores": [50, 85, 95] } ]
These operators provide you with powerful tools for querying and manipulating MongoDB documents. If you have a specific use case or need further clarification, let me know!