MongoDB Case sensitive query and case insensitive searches – Guidelines

MongoDB Case sensitive query examples

Today in this article, we will see how to perform MongoDB Case sensitive query and case insensitive searches for the given criteria.

I shall be explaining the queries using Mongo Shell.

Today in this article, we will cover below aspects,

We shall see a $regex regular expression pattern.

This pattern provides the capabilities for pattern-matching strings in queries.

I have a sample MongoDB document as below in one of the collections.

Here we shall be trying to search the documents based on Case sensitive and insensitive search queries.

mongodb regex case insensitive

MongoDB Case sensitive query

Let’s build a regex query for getting Author names where the name contains a word like “J” or “j” for the Author field depending on the search case sensitivity defined by the user.

You can update the MongoDB case insensitive exact match query or MongoDB aggregate match case insensitive search.

Query pattern

MongoDB regex query with case-sensitive search

{ Field Name: {'$regex' : 'J'}}

The above query defines the query as case sensitive

Example Query 1

db.Books.find({Author:{'$regex' : 'J'}})

Mongo Query Result :

For the Author with “J”,

   > db.Books.find({Author:{'$regex' : 'J'}})
   { "_id" : ObjectId("5db5a4476997188b2722c820"), "Name" : "Design Patterns", "Price" : 54.93, "Category" : "Computers", "Author" : "Ralph Johnson" }
   { "_id" : ObjectId("5ff503cda29ce9564c2724e3"), "Name" : "Code Refactoring", "Price" : 43.15, "Category" : "Computers", "Author" : "Julia Robert" } 

The above result produces the list of all books which has Author name contains “J” only

For the Author with “j”,

> db.Books.find({Author:{'$regex' : 'j'}})
 { "_id" : ObjectId("5f34a2430c3ca98a8c9052d2"), "Name" : "Design Patterns", "Price" : 54.93, "Category" : "Computers", "Author" : "Ralph johnson" }

As we can see the result produces the list of all books which has the Author name containing “j”

MongoDB Query with Case Insensitive

MongoDB Regex Query with Case insensitive can be defined using the $options pattern.

Here if the query is defined with “$option” with the value of “i” then the query will consider the query as case insensitive in nature.

MongoDB regex query with case-insensitive search

{ Field Name: {'$regex' : 'string', '$options' : 'i'}}

Example Query  – Case insensitive

Let’s now use case insensitivquery using $options,

{ Author:{'$regex' : 'J', '$options' : 'i'}

Mongo Query Result :

  > db.Books.find({Author:{'$regex' : 'J', '$options' : 'i'}})
   { "_id" : ObjectId("5db5a4476997188b2722c820"), "Name" : "Design Patterns", "Price" : 54.93, "Category" : "Computers", "Author" : "Ralph Johnson" }
   { "_id" : ObjectId("5f34a2430c3ca98a8c9052d2"), "Name" : "Design Patterns", "Price" : 54.93, "Category" : "Computers", "Author" : "Ralph johnson" }
   { "_id" : ObjectId("5ff503cda29ce9564c2724e3"), "Name" : "Code Refactoring", "Price" : 43.15, "Category" : "Computers", "Author" : "Julia Robert" } 

mongodb case insensitive search in array

The above query being case insensitive in nature gives your result for both “J” and “j” together.

References:

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *