How to Check Field Contains a String in MongoDB

Tahseen Tauseef Feb 02, 2024
  1. Use regex in MongoDB
  2. Check if the Field Contains a String in MongoDB
  3. Check if the Field Contains a String (Case-Insensitive)
  4. Check if the Field Contains a String (No Result)
  5. Check if the Field Contains a Specific Starting String
  6. Check if the Field Contains a Specific Ending String
How to Check Field Contains a String in MongoDB

This post tackles how to use regex in MongoDB to determine if a field contains a string or not.

Use regex in MongoDB

The regular expression (regex) is a text string that defines a search pattern. Several regex engines are written in slightly different syntax, but the fundamentals are the same.

A regex expression is a string (series of characters) contained on both sides by a single slash (/) like in the following example.

db.collection.find({
    "k": /pattern/
  })

But currently, as shell regex doesn’t work in query for mongoplayground.net, you can use the expression given below instead.

db.collection.find({
    "k": {
      "$regex": "pattern"
    }
  })

The $regex operator in MongoDB is used to find a query that contains a string. MongoDB allows you to use a regular expression to look for a pattern in a string via a query.

The $regex operator is used to pattern match text in queries, using regular expression capabilities.

Syntax:

db.collection.find({"k":{$regex:"pattern"}});

You can display the documents of those fields containing the value string that you can pass in the $regex regular expression operator. To understand the syntax above, you can create a collection of teams with the following document.

db={
  "teams": [
    {
      team: "Manchester City ",
      position: "1st",
      points: 70
    },
    {
      team: "Liverpool",
      position: "2nd",
      points: 69
    },
    {
      team: "Chelsea",
      position: "3rd",
      points: 59
    },
    {
      team: "Arsenal",
      position: "4th",
      points: 54
    },
    {
      team: "Tottenham",
      position: "5th",
      points: 51
    },
    {
      team: "Manchester United",
      position: "6th",
      points: 50
    },

  ]
}

Check if the Field Contains a String in MongoDB

You can use the following code to check if any document contains the string Man in the team field.

Query:

db.teams.find({
  team: {
    "$regex": "Man"
  }
})

Output:

[
  {
    "_id": ObjectId("5a934e000102030405000000"),
    "points": 70,
    "position": "1st",
    "team": "Manchester City "
  },
  {
    "_id": ObjectId("5a934e000102030405000005"),
    "points": 50,
    "position": "6th",
    "team": "Manchester United"
  }
]

Check if the Field Contains a String (Case-Insensitive)

You can use regex in MongoDB to check whether a field contains a specific case insensitive string value. Use the $i option with a regular expression in MongoDB.

The $i option will help you find fields with lower case and uppercase specified string values.

Query:

db.teams.find({
  team: {
    "$regex": "MAN",
    "options": "$i"
  }
})

Output:

[
  {
    "_id": ObjectId("5a934e000102030405000000"),
    "points": 70,
    "position": "1st",
    "team": "Manchester City "
  },
  {
    "_id": ObjectId("5a934e000102030405000005"),
    "points": 50,
    "position": "6th",
    "team": "Manchester United"
  }
]

Check if the Field Contains a String (No Result)

If a field doesn’t contain the specific string you searched for, you will receive no document found. For example, suppose you are using the following query.

Query:

db.teams.find({
  team: {
    "$regex": "Wolves"
  }
})

Output:

no document found

Since no document contains the string Wolves in the team name, you will get the output no document found.

Check if the Field Contains a Specific Starting String

We’ll use regex in MongoDB to examine if a field starts with a specific string value.

Query:

db.teams.find({
  team: {
    "$regex": "^C"
  }
})

Output:

[
  {
    "_id": ObjectId("5a934e000102030405000002"),
    "points": 59,
    "position": "3rd",
    "team": "Chelsea"
  }
]

You have successfully found one document that starts with the letter "C" with the help of regex in MongoDB.

Check if the Field Contains a Specific Ending String

Let’s check if a field ends with a specified string value in this example.

Query:

db.teams.find({
  team: {
    "$regex": "l$"
  }
})

Output:

[
  {
    "_id": ObjectId("5a934e000102030405000001"),
    "points": 69,
    "position": "2nd",
    "team": "Liverpool"
  },
  {
    "_id": ObjectId("5a934e000102030405000003"),
    "points": 54,
    "position": "4th",
    "team": "Arsenal"
  }
]

You have found two documents that end with the letter "l" with the help of regex in MongoDB.

Through this article, you have learned examples of different methods using $regex in MongoDB to check if a field contains a string or not.