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

OperatorDescriptionExampleExpected Output
$eqMatches values equal to a specified value.{ age: { $eq: 25 } } — Finds all documents where age is 25.[ { _id: 1, age: 25 } ]
$neMatches values not equal to a specified value.{ age: { $ne: 25 } } — Finds all documents where age is not 25.[ { _id: 2, age: 30 } ]
$gtMatches values greater than a specified value.{ age: { $gt: 20 } } — Finds all documents where age is greater than 20.[ { _id: 2, age: 30 } ]
$gteMatches 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 } ]
$ltMatches values less than a specified value.{ age: { $lt: 30 } } — Finds all documents where age is < 30.[ { _id: 1, age: 25 } ]
$lteMatches values less than or equal to a specified value.{ age: { $lte: 25 } } — Finds all documents where age is <= 25.[ { _id: 1, age: 25 } ]
$inMatches 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 } ]
$ninMatches 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

OperatorDescriptionExampleExpected Output
$andJoins 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 } ]
$orJoins 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 } ]
$notInverts the effect of a query clause.{ age: { $not: { $gte: 30 } } } — Finds all documents where age is not >= 30.[ { _id: 1, age: 25 } ]
$norJoins 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

OperatorDescriptionExampleExpected Output
$existsMatches documents that have the specified field.{ field: { $exists: true } } — Finds all documents where field exists.[ { _id: 1, field: 'value' } ]
$typeMatches documents with a specific field type.{ field: { $type: 'string' } } — Finds all documents where field is a string.[ { _id: 1, field: 'value' } ]

Array Operators

OperatorDescriptionExampleExpected Output
$allMatches arrays containing all specified elements.{ tags: { $all: ['tech', 'blog'] } } — Finds all documents where tags contains tech and blog.[ { _id: 1, tags: ['tech', 'blog', 'news'] } ]
$sizeMatches arrays with the specified size.{ tags: { $size: 3 } } — Finds all documents where tags contains exactly 3 elements.[ { _id: 1, tags: ['tech', 'blog', 'news'] } ]
$elemMatchMatches 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

OperatorDescriptionExampleExpected Output
$setSets the value of a field.{ $set: { name: 'John' } } — Updates name to 'John'.[ { _id: 1, name: 'John' } ]
$unsetRemoves a field from the document.{ $unset: { name: '' } } — Removes the name field.[ { _id: 1 } ]
$incIncrements the value of a field.{ $inc: { count: 1 } } — Increments count by 1.[ { _id: 1, count: 6 } ]
$pushAdds an element to an array.{ $push: { tags: 'new' } } — Adds 'new' to the tags array.[ { _id: 1, tags: ['tech', 'blog', 'new'] } ]
$pullRemoves 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!