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.

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.

Syntax:

db.collection.find({   key  : { $regex: /value/i } })
  • The find () method in MongoDB selects documents in a collection that matches the specified conditions and returns a cursor to the selected documents
  • $regex operator is used to search for the given string in the specified collection.

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.

Syntax:

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

Or

db.collection.find({ key : { $regex : 'pattern', $options: ' $i' }})

Example:

In the example we are considering the w3wiki database with Student Collection. We have inserted three document with rollno,Name , favSub as the field values.

 use w3wiki;

db.createCollection("Student");

db.Student.insertMany([{rollno:1,Name: "Jay",favSub:"Maths"},{rollno:2,Name :"Om", favSub: " maths"}, { rollno:3 ,Name : " Hari", favSub: "Science"}]);

We use regex operator with “i flag ” to perform case insensitive search.

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

Output:

Using the $regex operator with the ‘i’ flag

Explanation: In the above example we perform case insensitive search to search the favSub with value “maths” as string. So, we pass a regular expression with search value with option(i.e., {$regex : /maths/i}) for the favSub field in the find() method. In the regular expression, i flag is used to match both lower case and upper case pattern in the document.

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.

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.

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.
 db.Student.find({ favSub: { $regex: new RegExp(subject, 'i') } });

Output:

Using the $regex operator with a variable

Explanation: In the above example we use $regex operator along with a variable to perform the pattern matching .Inially subject variable is assigned the “Maths” value.Then the variable is passed in the RegExp() constructor along with the “i” flag to perform the case-insensitive search.As the collection contains two documents with favSub field with “maths” as value they are returned as output.

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