MongoDB Like query with example – Guidelines
Today in this article, we shall see how to write a MongoDB Like query with example ( resembles SQL ‘like’ query). We shall see a few scenarios with examples and query patterns.
Today in this article, we will cover below aspects,
We shall see how we can use just simple to complex queries or regular expression patterns for matching character combinations in strings.
Getting started
I have a sample MongoDB document as below in one of the collections. Here we shall be trying to search all the documents using similar to “Like“.
[
{
"_id": "5db5a4476997188b2722c820",
"Name": "Design Patterns",
"Price": 54.93,
"Category": "Computers",
"Author": "Ralph Johnson"
},
{
"_id": "5ff50353a29ce9564c2724e2",
"Name": "Design Patterns",
"Price": 54.93,
"Category": "Computers",
"Author": "Mike Casper"
},
{
"_id": "5ff503cda29ce9564c2724e3",
"Name": "Design Patterns",
"Price": 54.93,
"Category": "Computers",
"Author": "Julia Robert"
},
{
"_id": "5f34a2430c3ca98a8c9052d2",
"Name": "Design Patterns",
"Price": 54.93,
"Category": "Computers",
"Author": "Ralph Johnson"
}
]
We shall now try a few sets of like in MongoDB queries.
Scenarios 1 – MongoDB Query with ‘Like’ criteria elsewhere in the field
Let’s build a query for getting Author names where the name contains a word like “J” for the Author field.
Assumptions:
“J” could be contained at the start or last or in the middle or anywhere else of Author names
Query pattern
{ <Field Name> : /.your string ./ }
Where ‘J’ is your search criteria.
Example Query
{ Author: /.J./ }
Mongo Shell results,
> db.Books.find({Author:/*.J.*/})
{ "_id" : ObjectId("5db5a4476997188b2722c820"), "Name" : "Design Patterns", "Price" : 54.93, "Category" : "Computers", "Author" : "Ralph Johnson" }
{ "_id" : ObjectId("5f34a2430c3ca98a8c9052d2"), "Name" : "Design Patterns", "Price" : 56.00, "Category" : "Computers", "Author" : "Mike Johnson" }
Scenarios 2 – MongoDB Query with ‘Like’ that start with some criteria
let’s build a query to Get the list of items that start with some search criteria. Example “Ro” for Author field
Query pattern
{ Field Name : /^Ro/ }
Where ‘Ro’ is your search criteria.
Note: Above query has case sensitive query
Example Query
db.Books.find( { Author:/^Ro/ } )
Mongo Shell results,
> db.Books.find({Author:/^Ro/})
{ "_id" : ObjectId("5db5a4476997188b2722c821"), "Name" : "Clean Code", "Price" : 43.15, "Category" : "Computers", "Author" : "Robert C. Martin" }
Above query, we have Author “Robert C. Martin” that matches with search criteria.
Scenarios 3 – Get the list of items that end with some criteria
let’s build a query to Get the list of items that ends with some search criteria. Example “rt” for Author field
Query pattern
{ Field Name : /rt$/ }
Note: example shows the case-sensitive query
Example Query
db.Books.find( { Author:/rt$/ } )
Mongo Shell results,
> db.Books.find({Author:/rt$/}) { "_id" : ObjectId("5ff503cda29ce9564c2724e3"), "Name" : "Code Refactoring", "Price" : 43.15, "Category" : "Computers", "Author" : "Julia Robert"}
Above query, we have Author “Julia Robert” that matches with search criteria.
Like query using Regex ?
I shall cover a few more queries using regex in the next article,
Mongodb-like query case-insensitive
Please see the below article for more examples on MongoDB like query case-insensitive and sensitive queries.
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.