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.

OperatorDescriptionExample
$eqMatches values equal to a specified value.{ age: { $eq: 25 } }
$neMatches values not equal to a specified value.{ age: { $ne: 25 } }
$gtMatches values greater than a specified value.{ age: { $gt: 25 } }
$gteMatches values greater than or equal to a specified value.{ age: { $gte: 25 } }
$ltMatches values less than a specified value.{ age: { $lt: 25 } }
$lteMatches values less than or equal to a specified value.{ age: { $lte: 25 } }
$inMatches any value in a specified array.{ age: { $in: [25, 30, 35] } }
$ninMatches none of the values in a specified array.{ age: { $nin: [25, 30, 35] } }

2. Logical Operators

These are used to combine multiple conditions.

OperatorDescriptionExample
$andJoins queries with a logical AND.{ $and: [ { age: { $gt: 20 } }, { age: { $lt: 30 } } ] }
$orJoins queries with a logical OR.{ $or: [ { age: { $lt: 20 } }, { age: { $gt: 30 } } ] }
$notInverts a query condition.{ age: { $not: { $gt: 25 } } }
$norMatches 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.

OperatorDescriptionExample
$existsChecks if a field exists or not.{ name: { $exists: true } }
$typeMatches fields of a specific BSON type.{ age: { $type: "int" } }

4. Array Operators

These are used to work with arrays.

OperatorDescriptionExample
$allMatches arrays containing all specified elements.{ tags: { $all: ["sports", "music"] } }
$elemMatchMatches arrays containing at least one element that matches all specified conditions.{ scores: { $elemMatch: { $gte: 80, $lte: 90 } } }
$sizeMatches arrays with the specified number of elements.{ tags: { $size: 3 } }

5. Evaluation Operators

These are used to apply functions or custom conditions.

OperatorDescriptionExample
$regexMatches strings with a regular expression.{ name: { $regex: /^A/ } }
$exprAllows using aggregation expressions in queries.{ $expr: { $gt: ["$age", 30] } }
$jsonSchemaValidates documents against a JSON schema.{ $jsonSchema: { properties: { age: { type: "number" } } } }

6. Update Operators

These modify documents during an update operation.

OperatorDescriptionExample
$setSets the value of a field.{ $set: { name: "John" } }
$unsetRemoves a field.{ $unset: { name: "" } }
$incIncrements a field by a specified value.{ $inc: { age: 1 } }
$renameRenames a field.{ $rename: { oldName: "newName" } }
$pushAdds an element to an array.{ $push: { tags: "newTag" } }
$pullRemoves elements from an array.{ $pull: { tags: "oldTag" } }
$addToSetAdds a value to an array if it doesn’t exist.{ $addToSet: { tags: "uniqueTag" } }

7. Aggregation Operators

These are used in aggregation pipelines.

OperatorDescriptionExample
$matchFilters documents to pass only those that match the condition.{ $match: { age: { $gte: 30 } } }
$groupGroups documents by a specified key and performs aggregations.{ $group: { _id: "$city", total: { $sum: 1 } } }
$projectShapes the documents to return specific fields.{ $project: { name: 1, age: 1 } }
$sortSorts the documents.{ $sort: { age: -1 } }
$lookupPerforms 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!