MongoDB Evaluation Operators


Evaluation Operators in MongoDB

Evaluation operators in MongoDB are used to perform operations on the values of fields within documents. These operators allow you to evaluate or transform the data to perform complex queries, aggregations, and updates. Evaluation operators are commonly used in query conditions, projections, and aggregation pipelines.

Here’s an overview of some key evaluation operators in MongoDB:

1. $expr

The $expr operator allows you to use aggregation expressions within the find query. This enables you to perform complex queries using MongoDB’s aggregation framework within a query.

Syntax:

db.collection.find({ $expr: { expression } })

Example: Find documents where the price field is greater than the cost field:

db.products.find({ $expr: { $gt: ["$price", "$cost"] } })

2. $where

The $where operator allows you to use JavaScript expressions to query documents. This is less efficient compared to other query operators because it evaluates each document using JavaScript.

Syntax:

db.collection.find({ $where: "JavaScriptExpression" })

Example: Find documents where the price field is greater than 100 using JavaScript:

db.products.find({ $where: "this.price > 100" })

3. $type

The $type operator matches documents where the value of a field is of a specified BSON data type.

Syntax:

db.collection.find({ field: { $type: type } })

Example: Find documents where the age field is of type number:

db.users.find({ age: { $type: "number" } })

4. $mod

The $mod operator performs a modulo operation and matches documents where the result of the modulo operation on a field matches a specified value.

Syntax:

db.collection.find({ field: { $mod: [divisor, remainder] } })

Example: Find documents where the number field when divided by 3 has a remainder of 1:

db.numbers.find({ number: { $mod: [3, 1] } })

5. $regex

The $regex operator allows you to perform pattern matching using regular expressions.

Syntax:

db.collection.find({ field: { $regex: pattern, $options: 'options' } })
  • pattern: The regular expression pattern.
  • $options: Optional flags, such as 'i' for case-insensitive matching.

Example: Find documents where the name field contains the substring "john" (case-insensitive):

db.users.find({ name: { $regex: /john/, $options: 'i' } })

6. $text

The $text operator performs text search on indexed fields, supporting full-text search features like tokenization and stemming.

Syntax:

db.collection.find({ $text: { $search: "text" } })

Example: Find documents containing the word "MongoDB" in the description field (assuming a text index exists on description):

db.products.find({ $text: { $search: "MongoDB" } })

7. $geoNear

The $geoNear operator is used in aggregation pipelines to find documents near a specified point in a geospatial index.

Syntax:

db.collection.aggregate([ { $geoNear: { near: { type: "Point", coordinates: [longitude, latitude] }, distanceField: "distance" } } ])

Example: Find documents near a specific location (latitude: 40.7128, longitude: -74.0060):

db.places.aggregate([ { $geoNear: { near: { type: "Point", coordinates: [-74.0060, 40.7128] }, distanceField: "distance", spherical: true } } ])

8. $jsonSchema

The $jsonSchema operator allows for schema validation within queries, ensuring that documents match a specified JSON schema.

Syntax:

db.collection.find({ $jsonSchema: { schema } })

Example: Find documents where the age field is an integer and status is one of "active" or "inactive":

db.users.find({ $jsonSchema: { bsonType: "object", properties: { age: { bsonType: "int" }, status: { enum: ["active", "inactive"] } } } })