Updated March 6, 2023
Definition of Mongodb where
Mongodb where the operator is used to match and display the documents which were satisfying the condition of the JavaScript function or expression which was used in our query. The query contains the function of JavaScript or the expression of JavaScript that can be pass using where operator in MongoDB. We have used JavaScript expression or function parameter while using where operator in MongoDB, we can pass one of the parameters at the time of using where, operator. JavaScript function or expression using where the operator is referred as “this” or “obj” in MongoDB.
Syntax:
Below is the syntax of where operator in MongoDB.
Db.collection_name.find ({$where: <JavaScript expression (String) | JavaScript Function>})
Parameter description syntax of where operator in MongoDB.
1) Collection name –
This is defined as the name of collection which was used to retrieve data from the collection using the where operator. We need to specify the collection name in the find method to retrieve the data using where operator in MongoDB.
2) Find – This is used to query data from collections by using the where operator. We can use the pretty method find to the retrieved results of documents in a structured format.
3) Where – This is where the operator is used to display the result of matched documents. We have to pass the JavaScript function or expression using the where operator to display the result of documents.
4) JavaScript expression – Using where operator we have passing the JavaScript expression to return the result of matched documents from the collection. The type of this parameter while using where the operator is a string.
5) JavaScript function – Using the where operator we have passing the JavaScript function to return the result of matched documents from the collection. The JavaScript function or JavaScript expression is a mandatory parameter while using where operator in MongoDB.
How where work in Mongodb?
We have used JavaScript functions like map, md5, isString, ISODate, hex_md5, emit, gc, UUID, version, NumberLong, NumberInt in where operator.
There are multiple JavaScript functions are available in MongoDB which was we have using in where operator to display the result of matched documents.
Also below are the available properties which were we have used in where operator.
1) Args
2) MaxKey
3) MinKey
I suppose we have not found any matched document from our function, which was we have used with where operator, then where an operator is returning the empty result set.
In the below example we can see that we have not found a name with a given specified hexadecimal string, so where operator has not displayed any result in output.
Code:
db.where_test.find ()
db.where_test.find ( { $where: function() { return (hex_md5(this.name) == "9b53e667f30cd329dca1ec9e6a83e99") } } );
Figure – Where operator will return the empty result set when not found any matching document from the collection.
In the above example, we have used the function name as hex_md5, its comparing value for the name field with MD5 hash value. After comparing all the documents it will return the result of matched documents.
We need to use the find method with where operator to display the result of matched documents in MongoDB.
Find method using where operator will display the result in an unstructured format, to display the result in the structured format we need to use pretty method with find method.
MongoDB version 4.4 and later where operator not supporting for deprecated JavaScript BSON type code. It will only support the BSON type string or JavaScript BSON type code.
In general, we should use where operator when we cannot use another operator, or we cannot give the expected result from another operator.
Where operator will do a full scan on collection to find results from the query. It will scan each document to find the matching documents from the collection.
Where operator is used to providing better flexibility in MongoDB.
Where operator is not accessing any properties or global functions like db which was available in the mongo shell.
Example
Below is the example of where operator in MongoDB.
1) Where operator using hex_md5 function without using hash value –
In the below example, we have used hex_md5 function with where operator. But we have not used any hash value for matching the documents.
We can see that without using any hash value, hex_md5 function will return all the documents from the collection.
We have used the name field using where operator, but all the documents were returned because we have not used any hash value to compare with the name field.
Code:
db.where_test.find ( { $where: function() { return (hex_md5(this.name)) } } );
db.where_test.find ()
Figure – Example of where operator using hex_md5 function without using the hash value.
2) Where operator using hex_md5 function using hash value –
In the below example, we have used hex_md5 function with where operator. Also, we have used hash values for matching the documents. We have used “9b53e667f30cd329dca1ec9e6a83e994” hash value to find matching documents from collection.
We can see that using hash value, hex_md5 function will return only the matching documents from the collection.
We have used the name field with where operator.
Code:
db.where_test.find ( { $where: function() { return (hex_md5(this.name) == "9b53e667f30cd329dca1ec9e6a83e994") } } );
db.where_test.find ()
Figure – Example of where operator using hex_md5 function using the hash value.
3) Where operator using JavaScript expression –
In the below example, we have used JavaScript expression with where operator. We have used JavaScript expression to match the documents from the name and username field.
Using below expression we can see that only matching document will be returned which was same on both the field.
We have used a pretty method with find method to return the result in a structured format.
The name and username field is used to retrieve the matching documents from the where_test collection. In the output, we can see that three documents is returned which value is same on both the field.
Code:
db.where_test.find ( { $where: function() { return (this.name == this.username ) } } ) .pretty ();
db.where_test.find ()
Figure – Example of Where operator using JavaScript expression.
Conclusion
Where operator is more useful in MongoDB when we need to retrieve matching documents from the collection. JavaScript function and JavaScript expression are used with where operator to display the matching document. We can use multiple JavaScript functions like MD5, hex_md5 to display the result from the collection using the where operator.
Recommended Articles
This is a guide to Mongodb where. Here we discuss the definition, syntax, How where work in Mongodb? Examples, and code implementation. You may also have a look at the following articles to learn more –