How to Full-Text Search in MongoDB

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

When you’re storing string data in MongoDB, there may be times when you need to perform a full-text search on this data. This functionality was first introduced in MongoDB with version 2.4. At that point, the feature was considered experimental; today, full-text search is a key component of MongoDB. In this tutorial, we’ll provide an overview of the full-text search feature and show you how to perform this type of search in MongoDB.

Prerequisite

Before attempting the examples in this tutorial, make sure that MongoDB is installed and configured on your machine.

Full-text Search Basics

A good way to understand the concept of full-text search is to think about a typical Google search. When we use Google, we find content by providing a series of text, strings, phrases or keywords; in return, a number of results will be returned. In MongoDB, full-text search allows you to perform complex queries that are similar to those you’d perform using a search engine. You can search for phrases and stemmed variations on a word, and it’s also possible to exclude certain “negated” terms from your results.

Here are a couple of common scenarios where full-text search plays a key role:

  • searching for a certain topic on the web, whether we search Wiki or Google

  • searching for a name or term within social networks

Create Sample Dataset

Now that we’ve gone over the basics of how full-text search works, let’s create a sample dataset that we’ll use for the examples in this tutorial.

The first step is to connect to your MongoDB server and perform the following commands:

1
2
3
4
5
6
7
8
> use persondb
switched to db person
> db.person.insertMany( [
 { _id : "1001", name: "Franklin Roosevelt", quote: "More than just an end to war, we want an end to the beginnings of all wars." },
 { _id : "1002", name: "peter Dale Scott", quote:"I guess that when you invade a nation of warlords, you end up having to deal with warlords." },
 { _id : "1003", name: "Robert E. Lee", quote: "What a cruel thing war is... to fill our hearts with hatred instead of love for our neighbors."},
 { _id : "1004", name: "William Tecumseh Sherman", quote : "War is cruelty. There is no use trying to reform it. The crueler it is, the sooner it will be over." }
 ] );

MongoDB will return a response when the insert operation is complete. The result should look something like this:

1
2
3
4
5
6
7
8
9
{
        "acknowledged" : true,
        "insertedIds" : [
                "1001",
                "1002",
                "1003",
                "1004"
        ]
}

To verify if the insert operation was a success, use the following command: “

db.person.find().pretty();

The output should look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
        "_id" : "1001",
        "name" : "Franklin Roosevelt",
        "quote" : "More than just an end to war, we want an end to the beginnings of all wars."
}
{
        "_id" : "1002",
        "name" : "peter Dale Scott",
        "quote" : "I guess that when you invade a nation of warlords, you end up having to deal with warlords."
}
{
        "_id" : "1003",
        "name" : "Robert E. Lee",
        "quote" : "What a cruel thing war is... to fill our hearts with hatred instead of love for our neighbors."
}
{
        "_id" : "1004",
        "name" : "William Tecumseh Sherman",
        "quote" : "War is cruelty. There is no use trying to reform it. The crueler it is, the sooner it will be over."
}

Create Text Index in MongoDB

In this section, we’ll create a text index. This functionality is provided by MongoDB to support text search on strings within MongoDB collections or documents.

We can use the following command to create a text index: db.person.createIndex({"quote":"text"})

The output will look like this:

1
2
3
4
5
6
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}

Full-Text Search Example

So far, we’ve created our sample dataset and created a text index that will help support our full-text search. In this section, we’ll go ahead and perform a basic full-text search.

Let’s try using the following query to find all person documents where the associated quote contains the word “war”:

1
 db.person.find( { $text: { $search: "war" } } ).pretty();

The results should look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
        "_id" : "1001",
        "name" : "Franklin Roosevelt",
        "qoute" : "More than just an end to war, we want an end to the beginnings of all wars."
}
{
        "_id" : "1004",
        "name" : "William Tecumseh Sherman",
        "quote" : "War is cruelty. There is no use trying to reform it. The crueler it is, the sooner it will be over."
}
{
        "_id" : "1003",
        "name" : "Robert E. Lee",
        "quote" : "What a cruel thing war is... to fill our hearts with hatred instead of love for our neighbors."
}

Conclusion

If you’re managing string content in MongoDB, you’ll want to harness the power of full-text search to make your queries more efficient and accurate. In this tutorial, we showed you how to perform a simple full-text search on a sample dataset. With these instructions and examples, you’ll be able to search text fields within your own MongoDB documents.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.