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.

Updated October 2026: an index and performance section, a Mongoose section with safe user input, and an FAQ.

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 2 – Case 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 (2026) best practices and guidelines for software design and development.



Does a MongoDB contains query use an index, and what should you use instead?

Not efficiently. An unanchored or case-insensitive regex can’t make good use of a regular B-tree index, so MongoDB scans the collection. Only case-sensitive patterns anchored to the start, such as /^john/, use the index well.

How do you check whether your query uses an index?

Run the query through explain() and read the winning plan. For the case-insensitive example, call db.Books.find({ Author: /john/i }).explain('executionStats') and look at the stage names.

  • COLLSCAN: MongoDB read every document in the collection. This is what you should expect for contains and i-flag patterns.
  • IXSCAN: MongoDB walked an index. You’ll see this for /^john/ when the field has a regular index.

In plain English: A regular index is like a phone book sorted by surname. You can jump straight to “Joh”, but you can’t find every name with “ohn” in the middle without reading every page. The analogy stops fitting slightly, because MongoDB can still use the index to narrow a prefix range.

Also, drop the .* wrapper. A pattern like /.*john.*/ is redundant, because an unanchored /john/ already means “contains”. The extra wildcards add nothing.

What can you use instead of a regex for contains?

It depends on what “contains” means for you. Here are the realistic options.

  • $text index: It matches whole, stemmed words only, so it can’t find “ohn” inside “Johnson”. A collection can have only one text index, and a query can use only one $text expression.
  • $text with $regex: You can’t put both inside an $or, because MongoDB returns an error. The workaround is a $match on $text, followed by $unionWith on a second pipeline with a regex $match.
  • Atlas Search: It’s built on Lucene and queried with the $search stage. A wildcard query such as *john* on the Author path is the recommended production option for substring search on Atlas.
  • Case-insensitive collation index: It’s an alternative to the i flag for exact-match lookups. The trade-off is that every query must specify the same collation to use the index. Regex matching doesn’t take advantage of collation, so this won’t speed up substring searches.

Atlas Search isn’t free. Its inverted index is read through a separate mongot process, and the index has to stay in sync with your data, which adds overhead. For a simple single-field match, the B-tree on a start-anchored regex may still be faster. Atlas Search tends to pay off when a query spans several indexed fields.

Warning: MongoDB 8.2 added public previews of Search and Vector Search for self-managed Community and Enterprise deployments. They aren’t recommended for production clusters, so don’t build a production contains feature on them yet.

Use a plain regex for small collections, admin tools and one-off queries. If you need fast contains search on a large collection, move to Atlas Search, or redesign the query so it can anchor at the start of the string.

Leave a Reply

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