Web development and Tech news Blog site. WEBISFREE.com

HOME > db

How to Use MongoDB $text Operator and Examples

Last Modified : 05 Oct, 2023 / Created : 05 Oct, 2023
View Count

Let's learn about the $text operator among MongoDB operators.

# What is the $text Operator?

When searching for text using MongoDB, the $text operator is frequently used. It assists in easily constructing search queries when searching for desired text in titles, bodies, or other text-rich parts of MongoDB.

The basic syntax for using the $text operator is as follows:
  "$text": {
    "$search": "<string>",
    "$language": "<string>",
    "$caseSensitive": <boolean>,
    "$diacriticSensitive": <boolean>

Explained simply, the components are as follows:

  • $search: keyword condition for the search
  • $language: language setting for the search
  • $caseSensitive: whether case is sensitive
  • $diacriticSensitive: whether to use diacritic marks for some languages

The $text operator in MongoDB has features like:

- Easily using included search terms
- Searching based on a matching Score
- Case sensitivity and language setting options

Let's explore in more detail below.

! Setting Up Text Index

A text index setup is essential when using the $text operator for searching. Here's how you use it if you set up an index on a field named “siteName”.
db.sites.createIndex({siteName: 'text'})

If you need to set up multiple fields, like “siteName” and “siteUrl”, you can do it as follows:
db.sites.createIndex({siteName: 'text', siteUrl: 'text'})

If you don't set up the appropriate index, you might encounter the following error when using the $text operator for searching:
// pymongo인 경우
pymongo.errors.OperationFailure: text index required for $text query

@ Setting Weights
You can set weights like this: For instance, if you’re setting up a text index on the “title” and “content” columns, you can add the weights option and use it as follows:
  {title: 'text', content: 'text'},
  { weights:
      title: 6,
      content: 2

The value for “title” is 6, which is three times higher than the value for “content” (2), affecting the search results accordingly.

! Deleting Text Indexes

On the other hand, if you want to delete a set-up index, you use dropIndexes().
db.articles.dropIndexes({_fts: 'text', _ftsx:1});

Next, let's look at some simple queries used on the web.

For other MongoDB-related index information, refer to the link below.

Related Link >

! Viewing $text Operator Examples

Here are some examples of frequently used text searches.
@ Searching for the text 'webisfree'
db.sites.find( { $text: { $search: 'webisfree' } } )

@ Searching for text containing 'webisfree' or '웹이즈프리' (or condition)
db.sites.find( { $text: { $search: 'webisfree 웹이즈프리' } } )

When searching for multiple terms, you can separate them with spaces (whitespace). The code above finds search results that contain either 'webisfree' or '웹이즈프리'.

@ Searching for text containing both 'webisfree' and '웹이즈프리' (and condition)
db.sites.find( { $text: { $search: '"webisfree" "웹이즈프리"' } } )

This example searches for text that includes both terms, not using the 'or' condition but the 'and' condition. When you use quotes for searching, it searches for text containing all quoted terms.

@ Searching for 'webisfree' but excluding documents containing 'favorite'
db.sites.find( { $text: { $search: 'webisfree -favorite' } } )

If there’s text you want to exclude, use a minus sign (-). So, “-favorite” will return results that don’t include the word 'favorite'.

! Finding Search Terms Based on Score

Now, let's learn how to use scores when conducting text searches.
  { $text: { $search: 'webisfree' } },
  { score: { $meta: 'textScore' } }

The search result will display an added score field value.

So far, we've explored various ways to use the $text field in MongoDB.
Perhaps you're looking for the following text as well?