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.
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.
Contact Us