CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Mongo DB > MongoDB $expr Expressive Query Operator

MongoDB $expr Expressive Query Operator

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Published: September 10, 2022 ยท 3 min read ยท 0 Comments
Share: X in ๐Ÿ”—
$expr expressive Operator in MongoDB

The $expr operator in MongoDB is an Expressive Query Operator. It does more than the single operator like $and and $or. It allows the use of aggregation expressions within the query language.

The syntax of $expr operator is given below:

{ $expr: { <expression> }}

It also allows us to use variables and conditional statements. Let’s see an example of this.

In our previous example of comparing operators like $eq and $ne, we were comparing field values to the given values. But we can’t compare fields within the same document to each other with these operators. With the $expr operator, we can easily do that.

Let’s see the example from the given document below:

We have an employee collection and have the following documents:

{
        "_id" : ObjectId("62f77a41c41dfbf8c1e24e17"),
        "name" : "Coder Sathi",
        "department" : "Engineering",
        "salary" : 50000,
        "address" : {
                "city" : "Kathmandu",
                "area" : "Pashupatinath"
        }
}
{
        "_id" : ObjectId("631c96a4927abfef4bc0c1e7"),
        "name" : "Virat Kohli",
        "department" : "Engineering",
        "salary" : 50000,
        "address" : {
                "city" : "Delhi",
                "area" : "Kalkaji"
        },
        "bonus" : 40000
}
{
        "_id" : ObjectId("631c96ad927abfef4bc0c1e8"),
        "name" : "Radha Krishnan",
        "department" : "HR",
        "salary" : 50000,
        "address" : {
                "city" : "Pokhara",
                "area" : "Lakeside"
        },
        "bonus" : 50000
}

With the help of the above documents, let’s see which employee has the same amount of salary and bonus. To check the result we can use the $expr operator as given below.

Query

db.employee.find({"$expr": {"$eq":["$salary", "$bonus"]}}).pretty()

And the output is:

{
        "_id" : ObjectId("631c96ad927abfef4bc0c1e8"),
        "name" : "Radha Krishnan",
        "department" : "HR",
        "salary" : 50000,
        "address" : {
                "city" : "Pokhara",
                "area" : "Lakeside"
        },
        "bonus" : 50000
}

Cool!

The query we’ve written matches the exact one employee document whose salary and the bonus are equal.

There is one thing may look strange to you is that the $ sign with the field salary and bonus.

You may ask why the $ sign is there.

The $ sign in MongoDB query language has a very important use case. One of them is to denote the use of an operator and another addresses the value of the field rather than the field name itself.

In our query, $salary and $bonus indicate that the value of these is 50000. That is the reason MongoDB Query Language (MQL) is able to compare values between these fields.

Let’s take another example where we will be finding the list of employees whose salary is greater than the salary amount with the following query:

db.employee.find({"$expr": {"$gt":["$salary", "$bonus"]}}).pretty()

And the output is:

{
        "_id" : ObjectId("62f77a41c41dfbf8c1e24e17"),
        "name" : "Coder Sathi",
        "department" : "Engineering",
        "salary" : 50000,
        "address" : {
                "city" : "Kathmandu",
                "area" : "Pashupatinath"
        }
}
{
        "_id" : ObjectId("631c96a4927abfef4bc0c1e7"),
        "name" : "Virat Kohli",
        "department" : "Engineering",
        "salary" : 50000,
        "address" : {
                "city" : "Delhi",
                "area" : "Kalkaji"
        },
        "bonus" : 40000
}

Like in the earlier query, we are able to use $expr operator with other comparing operators as well.

Conclusion

In this post, we learned to use $expr an Expressive query operator in MongoDB.

Related Posts:

  • Control Statements in Java
  • Expressions in Java
  • MongoDB Group aggregation in Java
  • Array Operator in MongoDB
  • Increment and Decrement Operator in Java
  • Update MongoDB Document
Tags:mongodb
Was this article helpful?
โ† Previous ArticleHow To Make WordPress Admin Dashboard Faster?
Next Article โ†’Array Operator in MongoDB

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
  • Complete Guide to JaCoCo: How to Measure Java Code Coverage Accurately
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap