How to use where Operator in MongoDB

MongoDB provides a strong querying system supported by several commands and operators. There exists a long list of operators (like $size, $where, $gt, $regex, and many more) that have extended the MongoDB use to fulfill the basic functionality of any database. The $where operator belongs to the evaluation query operators class and can be exercised to pass a JavaScript-based string or JavaScript function. The $where operator is used in MongoDB to get only those documents that match JavaScript expressions.

In this descriptive post, we have provided an insight into the usage of $where operator in context of MongoDB.

How does $where work in MongoDB

It is noticed that the $where operator is used rarely as compared to other standard operators of MongoDB like $gt, $lt, $in, and $nin.

As mentioned earlier, the $where operator works for only JS-based strings or its functions only and the syntax to use $where operator is mentioned below:

{$where: <JS-string|JS-function>}

It is observed that $where operator cannot be executed with few standard functions of MongoDB like db. The $where operator, along with map-reduce operations in MongoDB, support several JavaScript functions, and thus they cannot be used globally

How to use $where in MongoDB

The following instances of MongoDB are used in this guide:

Database: The database used here is named “Linuxhint“.

Collection-name: The collection that is exercised in this article is named “grades“.

And the documents contained by “grades” collection are shown below:

> db.staff.find().pretty()

Text Description automatically generated

Note: C1, C2, C3, C4 are assumed as courses id’s in the above output.

Example 1: Basic use of $where operator

Before getting into details, you must remember that the two keywords, either “this” or “obj” are used to refer to documents in JS function or JS expression.

Referring to the documents in the “grades” collection:

Command 1: The command written below will search for documents and will display only those that have the same values in different fields:

As you can check that the output contains only “one” document where values of “C1” and “C2” match.

> db.grades.find({$where: "this.C1==this.C2"}).pretty()

Text Description automatically generated

Command 2: The same output(as in Command 1) can be achieved by issuing the below stated command in Mongo Shell. Here, the “obj‘ keyword is used instead of “this“.

> db.grades.find({$where: "obj.C1"=="obj.C2"}).pretty()

Text Description automatically generated

Command 3: You can also use the $where operator as we have performed in the command below. In the following command, a function() will return the documents obtained by applying “obj” and “this” keyword, the value of “C1” and “C3” matches.

> db.grades.find({$where: function(){return (this.C1==this.C3)}}).pretty()

Text Description automatically generated

Command 4: The application of JS function() with $where operator can also be achieved by using the “obj” keyword instead of “this“. For this, you can execute the following command:

> db.grades.find({$where: function(){return obj.C1==obj.C3}}).pretty()

Text Description automatically generated

Example 2: $where acts without using it in command

If your command only performs the application of the $where operator, then you can use the command without specifying the $where keyword in the command. The example command in a situation like these is stated below:

> db.grades.find("this.C1==this.C2").pretty()

Text Description automatically generated

Or the “obj” keyword can also be used instead of “this” in the above command.

> db.grades.find("obj.C1==obj.C2").pretty()

Text Description automatically generated

Example 3: Using $where with standard MongoDB operators

The $where operator can be used with several other operators of MongoDB. For example, in the below mentioned command, we have used less than(<) operator with $where operator. The command written below will look for conditions of both operators and then any document satisfying either “==” or “||”condition will be displayed in the output.

> db.grades.find("this.pos1==this.pos2||this.pos1 < this.pos2").pretty()

Text Description automatically generated

It is observed from working with the $where operator that is searching inside the bulk of documents may become time-consuming with the $where operator because MongoDB executes $where operator after any other standard operator used in the query.


MongoDB frequently updated its versions in the past, and the reason was to improve the performance and efficacy of any MongoDB command or method, or operator. In MongoDB, the $where the operator can be used to match the fields using JS expression or JS function. In this detailed guide, we have provided the usage of the $where operator in MongoDB. After detailed research and data collection, we came to the point that the alternatives of $where the operator should be preferred, as the $where operator searches for the whole collection before giving you the output.

About the author

Adnan Shabbir