04 / 19

What is Query Selectors/Query Operators?

In MongoDB, query selectors (also called query operators) are special expressions used to filter and retrieve documents from a collection based on specific conditions. They allow you to perform complex searches, comparisons, and logical operations on your data.

Comparison Operators:
  1. 1

    $eq: Matches values that are equal to a specified value.

  2. 2

    $gt: Matches values that are greater than a specified value.

  3. 3

    $gte: Matches values that are greater than or equal to a specified value.

  4. 4

    $in: Matches any of the values specified in an array.

  5. 5

    $lt: Matches values that are less than a specified value.

  6. 6

    $lte: Matches values that are less than or equal to a specified value.

  7. 7

    $ne: Matches all values that are not equal to a specified value.

  8. 8

    $nin: Matches none of the values specified in an array.

javascript
Logical Operators:
  1. 1

    $and: Joins query clauses with a logical AND returns all documents that match the conditions of both clauses.

  2. 2

    $not: Inverts the effect of a query predicate and returns documents that do not match the query predicate.

  3. 3

    $nor: Joins query clauses with a logical NOR returns all documents that fail to match both clauses.

  4. 4

    $or: Joins query clauses with a logical OR returns all documents that match the conditions of either clause.

javascript
AND as well as OR Conditions
Array Operators:
  1. 1

    $all: Matches arrays that contain all elements specified in the query.

  2. 2

    $elemMatch: Selects documents if element in the array field matches all the specified $elemMatch conditions.

  3. 3

    $size: Selects documents if the array field is a specified size.

javascript
javascript
Evaluation Operators: Evaluation operators in MongoDB allow you to evaluate the data in a document based on more complex logic, such as regular expressions, schema validation, or even custom JavaScript functions.
  1. 1

    $regex: Provides regular expression capabilities for pattern matching strings in queries.

  2. 2

    $mod: Performs a modulo operation on the value of a field and selects documents with a specified result.

  3. 3

    $text: Performs a text search on the content of the fields indexed with a text index. This is much more powerful than $regex for large blocks of text.

  4. 4

    $expr: Allows the use of aggregation expressions within the query language. This is powerful because it lets you compare two different fields within the same document.

  5. 5

    $jsonSchema: Validates documents against a given JSON Schema. While usually used for collection validation, it can be used in a query.

  6. 6

    $where: Passes a string containing a JavaScript function or a full JavaScript function to the query system.

javascript
Bitwise Operators: allow you to perform logic on the individual bits of integer values. For a senior developer, these are highly efficient tools for managing bitmasks, such as user permissions, feature flags, or status codes where multiple states are packed into a single number to save space.
  1. 1

    $bitsAllSet: Matches documents where all the bit positions specified in the query are set (1) in the document's field. Checking if a user has both Read and Write permissions.

  2. 2

    $bitsAnySet: Matches documents where at least one of the specified bit positions is set (1). Checking if a user has either Admin OR Superuser status.

  3. 3

    $bitsAllClear: Matches documents where all the bit positions specified are clear (0). Finding accounts that have no restrictions or blocks set.

  4. 4

    $bitsAnyClear: Matches documents where at least one of the specified bit positions is clear (0). Finding items that are missing at least one required validation flag.