04 / 19

What is Query Selectors/Query Operators?

Difficulty: 5/10
filter operators, array operators, projection 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.

Scenario Questions

0-2 years experience

  1. 1You need to retrieve all users whose age is greater than 30 and whose status is 'active'. How would you write the MongoDB query using the appropriate query selectors?
  2. 2If you run a find query with { price: { $gt: 100 } } and the collection has an index on price, what does MongoDB use to filter the documents?
  3. 3What happens if you accidentally use $in with a single scalar value instead of an array? How would the query behave?

2-5 years experience

  1. 1Your team added a new field 'tags' as an array of strings. You need to find documents where tags contain both 'urgent' and 'backend'. The current query returns too many results. Walk me through how you'd adjust the query operators to fix it.
  2. 2During a sprint we noticed a query that uses $or with multiple $regex conditions is causing a performance regression. How would you diagnose the issue and what operator changes might improve it?
  3. 3We have a query that uses $elemMatch to filter nested array objects, but it sometimes returns documents that don't actually match the inner criteria. What could be wrong with the selector and how would you correct it?

5-8 years experience

  1. 1Our analytics service aggregates logs stored in MongoDB. We need to filter on a timestamp range and a set of status codes, while also projecting only a few fields. Design the query pipeline, choosing between $match, $project, and appropriate operators, and explain the performance implications.
  2. 2We are considering replacing a series of $or conditions on a field with a $in operator to simplify the query. What are the trade‑offs in terms of index utilization and query planner behavior?
  3. 3A legacy system uses $where JavaScript expressions for complex filtering, but we need to migrate to pure query operators for scalability. How would you refactor a $where that checks multiple fields into an equivalent selector using query operators?

8+ years experience

  1. 1Our product is moving from a monolithic MongoDB deployment to a sharded cluster. How would you evaluate the impact of existing query selectors (e.g., $regex, $elemMatch) on shard key selection and cross‑shard routing?
  2. 2We have multiple microservices that each query the same collection with different selector patterns. How would you establish a shared query‑operator strategy or abstraction to ensure consistency, maintainability, and optimal index usage across teams?
  3. 3During a data migration, we need to rewrite documents to a new schema where some fields are nested deeper. How would you design a bulk update that safely transforms existing queries using operators like $set, $rename, and $arrayFilters, while minimizing downtime?

Follow-up Questions

  • Can you give an example of when you'd prefer $elemMatch over a simple field match?
  • How does MongoDB decide which index to use with a compound query involving $gt and $in?
  • What monitoring metrics would you watch after changing a selector to improve performance?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.