How to find a document in mongo using partial value of a field

MongoDB find syntax is pretty simple. Lets have a collection named users and find all the documents in the collection,

// Query
db.users.find({})

// Result
;[
  {
    email: 'one@email.com',
    name: 'One for like and comment',
  },
  {
    name: 'Two for Comments',
  },
  {
    email: 'three@email.com',
    name: 'Three for Like',
  },
]

If we need to filter by some field then,

// Query
db.users.find({ name: 'One for like and comment' })

// Result
;[
  {
    email: 'one@email.com',
    name: 'One for like and comment',
  },
]

What if we need to filter by the field but we don't know the exact case or exact value. Let's see the example

  • list all the document which has a word like in the field name

Filter document using regular expression

It can be easily achieved using regular expression instead of string value,

// Query
db.users.find({ name: /like/ })

// Result
;[
  {
    email: 'one@email.com',
    name: 'One for like and comment',
  },
]

Here /like/ is the regex which will find all the word which match like. But this query is case sensitive. It won't match Like. But its very easy to write case insensitive query,

db.users.find({ name: /like/i })

// Result
;[
  {
    email: 'one@email.com',
    name: 'One for like and comment',
  },
  {
    email: 'three@email.com',
    name: 'Three for Like',
  },
]

Adding i at the end of regex denotes match the word irrespective of the case. (uppercase, lowercase, etc).

We can use any regex based search to filter the values in mongoDB. We will learn more regex and mongoDB tricks soon.

MongoDB is very powerful and it provides a lot of methods to query what you exactly want. Hope you find this tutorial helpful 🤗

Beginners to ProNode Js

Visual Guide to API Design Best Practices

This visual eBook covers essential best practices for designing robust APIs using REST principles.

This book is ideal for beginners and backend developers seeking to enhance their API design skills. However, it is not suited for those seeking an in-depth exploration of API design. This book is a quick read under 40 slides like scrolling through your instagram feed.

Visual Guide to API Design Best Practices