How to use the $regex Operator Directly In MongoDB

  • $regex operator is used to search for the given string in the specified collection.If we use the regex operator directly in the find method then the find method return the matched document.
  • It performs the case sensitive search without flag which returns the completely matched documents.

Syntax

db.collection.find({   key  : { $regex: /value/ } })

Example

 db.Student.find({favSub : {$regex:/maths/}});

Output:

Using the $regex operator directly

Explanation: In the above example the w3wiki database contains the Student Collection with 3 document .From 3 documents 2 documents contains the maths as the favourite Subject (favSub field) but only one document is returned as we didn’t use any option or flag .If use regex operator directly is performs the case sensitive search.

MongoDB Query with Case Insensitive Search

MongoDB, a leading NoSQL database, stores data in a flexible JSON-like format called BSON. In MongoDB, keys are stored as strings, and values can be strings, numbers, or objects. Sometimes there is a need to search for data without considering the case of the strings. This is where a case-insensitive search is needed.

In this article, we will explore how to perform case-insensitive searches in MongoDB using the $regex operator and the ‘i’ flag with the find() method in detail by understanding various methods along with the output and so on.

Similar Reads

Case Insensitive Search in MongoDB

In MongoDB data is stored in documents in key-value pairs in BSON format. The key is a string and the value of the key can be a string number or object. The string value can be in uppercase or lowercase letters. To perform the case-insensitive search we use the find() method with the regex operator and a flag. We will understand the below approaches for better understanding with the help of examples....

1. Using the $regex Operator with ‘i’ Flag

$regex operator is used to search for the given string in the specified collection. To find a document with a specific value without considering the case, we use the regex operator with an i flag. The i flag is used to match both lower case and upper case patterns in the given document. We use the regex operator in the find() method to return the specific matched documents from the collection....

2. Using the $regex Operator Directly

$regex operator is used to search for the given string in the specified collection.If we use the regex operator directly in the find method then the find method return the matched document. It performs the case sensitive search without flag which returns the completely matched documents....

3. Using the $regex Operator with Variable

$regex operator is used to search for the given string in the specified collection.We can pass a variable to regex operator to search for the particular value. To pass a variable we use RegExp() constructor which is used to perform pattern matching. Along with a variable we can also pass a flags. If we pass i flag then it is used to match both lower case and upper case pattern in the given document. We use the regex operator in the find() method to return the specific matched documents from the collection....

Conclusion

We have learnt how to perform the case insensitive search. To perform the case insensitive search we considered i flag with regex operator. To also studied how to use regex operator directly, how to use flag along with the regex operator and how to use variable in the RegExp constructor. By learning case sensitive and case insensitive search we can find documents from any type of collection....

Contact Us