MongoDB Get Record That Contains A String

MongoDB Get document That Contain a String

Today in this article, we will see how to perform the MongoDB Contain a String query with examples. This specified string you may want to match exactly or partially and get all the matching documents from the MongoDB Database. We will also see how to match documents based on case-sensitive and insensitive searches.

We will see multiple approaches to achieve the same like using like query or regex query etc.

Let’s take the example below to learn the query.

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

[
  {
    "_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": "Mike Johnson"
  }
]

MongoDB Contain a String- Approach 1

Let’s build a MongoDB query using a regex for getting documents that Contain a String.

Author names where the name contains a word like “John” or “john” for the Author field value.

Here we are trying to match “John” or “john” could be contained at the start or last or in the middle or anywhere else of Author names.

Command

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

Example

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

Mongo Query Result :

   db.Books.find({Author:{'$regex' : 'J'}})
   { "_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" : "Mike Johnson" } 

Query 2Case insensitive

Command

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

Let’s now use case insensitive query using $options,

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

MongoDB Get Record That Contain a String

MongoDB Get document That Contains a String – Approach 2

Command

{ <Field Name> : /.your string ./ }

Example

 { Author: /.John./ } 

The above query will return all the documents where the Author name contains a string called “John”. “John” may be available in the value in the start or middle or end of the values.

  > 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" }  

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 *