Use aggregation expressions in queries with $expr

A comparison between two amounts using $expr

With MongoDB 3.6 the query language gains a new level of expressivity: you can now make use of aggregation expressions in a query using the $expr operator. This feature allows you to take full advantage of all expression operators within all queries, much of which previously had to be done within application logic or was restricted to the aggregation pipeline. $expr offers better performance than the $where operator, which while still available isn't as desirable because it uses the server-side JavaScript engine.

Use aggregation expressions with $expr

The $expr operator can be used in the query portion of all find, update, and delete operations, as well as in the $match stage in aggregations. A valid expression can contain:

For example, an application may want to give users an amount of free credits per month and keep track of all expenses for the same account in an array.

To find the accounts whose overall expenses have exceeded the credits for that account you can run this query:

Use $expr with caution
Prior to v3.6, the $gt operator could be used in aggregation queries to compare fields with given values or with other fields in the same documents. However, comparing fields with other fields was previously not possible in a query. To do this, users had to do the comparisons within their application logic.

Usage of $where came with the caveat that the query would evaluate documents, not index entries. Unfortunately, index entries can't always be used to satisfy $expr filters either, so be conscious of how much of the collection's data will be processed. You can get a sense of the number of processed documents by running MongoDB's .explain() command. Depending on the memory needs of an $expr operation, you may want to consider carefully whether to use a $expr query or to perform equivalent operations within the application. In this expanded example, we limit our query to a specific day's worth of data using the {date: 1} index, rather than operating on the whole collection at once:

Learn more

To read more about this check out the documentation: https://docs.mongodb.com/manual/reference/operator/query/expr/.

If you're interested in more MongoDB tips, follow us on Twitter @mlab. Also, if you could use help with an mLab-hosted database, you can always contact support@mlab.com.

Naomi Pentrel is a Developer Advocate at mLab and a full-stack pythonista with a focus on improving developer workflows. She has previously worked at companies such as Google, Microsoft, and Bloomberg. When not at work or a organizing Smooth DevOps, Naomi is an avid Pokémon master and a slayer of houseplants.

About Naomi Pentrel

Naomi Pentrel is a Developer Advocate at mLab and a full-stack pythonista with a focus on improving developer workflows. She has previously worked at companies such as Google, Microsoft, and Bloomberg. When not at work or a organizing Smooth DevOps, Naomi is an avid Pokémon master and a slayer of houseplants.