• Post author:
  • Post category:MongoDB
  • Post last modified:May 9, 2024
  • Reading time:7 mins read
You are currently viewing MongoDB find() with $where Operator

This article mainly focuses on the where operator along with find() method and logical AND operator in MongoDB. The where is the parameter of the MongoDB find() method allows us to specify a JavaScript expression that is used to filter the documents returned by the query. This expression can reference the fields of the documents in the collection, as well as any functions defined in the expression.

Advertisements

The article provides some examples to illustrate the where operation of MongoDB. Let’s consider the collection student collection with a few documents and I will use this collection to explain the where parameter.


# Create Collection
db.student.insertMany([
      {  
         _id: 1,
         name: "Mickel",
         age: 24,
         gender: "Male",
         course: ["Python", "MongoDB"]
 
      },
      {
         _id: 2,
         name: "Elena",
         age: 20,
         gender: "Female",
          course: ["Java", "MongoDB"]
      },
      {
         _id: 3,
         name: "Caroline",
         age: 25,
         gender: "Female",
         course: ["C++", "SQL"]
      },
      {
         _id: 4,
         name: "Elan",
         age: 23,
         gender: "Male",
         course: ["Java", "PHP"]
      }
   ]
)

1. Using $where Operator with find()

Here, we will find the documents where the specified field value matched with the field values of the documents. For this, we have used the following MongoDB query with the $where operator.


#find documents where specified field value matched with field values of documents
db.student.find({ $where: function() 
     { return this.name === "Mickel"; } 
})

The query here is specified with the find() method in MongoDB. Then uses the $where operator to find all documents in the student collection where the given field is equal to the specified value. The $where operator is set with a function() that is executed for each document in the collection.

The function() is deployed with the expression. This expression uses the this keyword to refer to the field name of the current document being evaluated. Also, the comparison operator(===) checks whether its name field is equal to the given string. After that, the documents for which the function returns true are returned by the find() method. The output yielded for this query is represented below.

 where operator in MongoDB

2. Where documents with a field value greater than a number

Moreover, the find where query of MongoDB can be specified with the conditional operator to find the document. Consider the following query which uses the expression of the greater-than operator.


#find where query specified with  conditional operator
db.student.find({ $where: function() { return this.age > 23; } })

The above find() query of MongoDB is applied on the collection student where it takes the $where operator as a parameter. Then, a JavaScript function is set in the where parameter to filter the documents. The function is specified to check whether the age field of the current document is greater than a particular value by using the greater-than sign (>).

Here, the this.age is used as a reference to the collection student. The document is included in the result set only when the function returns true; else, it is excluded. The output displayed all documents from the student collection where the age is greater than 23.

 where operator in MongoDB

A few examples of $where operator can also be found here.

3. Using $where with ANd Operator in MongoDB

In some cases, we can use where operator in MongoDB along with the AND operator to create complex queries that combine multiple conditions.


# where operator in MongoDB along with the AND operator
db.student.find( 
  { $where: "this.age > 24 && 
                  this.gender === 'Female'" 
  }
)

Here, the query is set with the find() method that is used to retrieve documents from the collection that match a given set of criteria. The $where operator is passed as an argument within the find() method. Then, the $where operator is set with a javascript expression that defines the criteria for selecting documents from the collection. The expression uses a && AND operator symbol to perform the logical operation of AND.

The && operator here indicates only those documents should be selected if the age field of the document is greater than the given field value. This is done by using the sign (>) greater than and the gender field of the document is set with the comparison operator (===) that should be equal to the given value. Hence, the output retrieves all documents from the student collection where the age field is greater than 24 and the gender field is equal to Female. Thus, the document is retrieved below which satisfied both the conditions of $where operator.

 where operator in MongoDB

4. Conclusion

In conclusion, we have explored some examples of where parameter in MongoDB. We have done this by calling it inside the find() method to query specific documents. In some circumstances when other operators are insufficient then $where operator may helpful.

More details about this topic can be found here.