{"componentChunkName":"component---src-pages-markdown-remark-fields-slug-js","path":"/engineering/full-text-search-in-mongodb/","result":{"data":{"markdownRemark":{"id":"c6611215-4458-5540-a382-1ff1548299c9","excerpt":"One of the leading NoSQL databases, MongoDB is well known for its fast performance, versatile schema, scalability and great capabilities for indexing. Let us…","html":"<p>One of the leading NoSQL databases, MongoDB is well known for its fast performance, versatile schema, scalability and great <a href=\"https://www.loginradius.com/blog/engineering/index-in-mongodb/\">capabilities for indexing</a>. Let us look at some context before we get into some details. Full-text search is an essential feature when we talk about finding content on the internet. A google search is the best example for this when we see the content using the phrases or keywords. In this article, we will learn about full-text search capabilities in MongoDB based on text index.</p>\n<h2 id=\"create-a-sample-database\" style=\"position:relative;\"><a href=\"#create-a-sample-database\" aria-label=\"create a sample database permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Create a Sample Database</h2>\n<p>Before we begin, we will create a sample database that will be used during the tutorial.</p>\n<p>We will create a database with the name <em>myDB</em> and create a collection with the name <em>books</em>. For this, the statement would be as follows.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt; use myDB</span>\n<span class=\"grvsc-line\">&gt; db.createCollection(&quot;books&quot;)</span>\n<span class=\"grvsc-line\">&gt;</span></code></pre>\n<p>Let's insert some documents by using the following statement.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt; db.books.insert([</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">      &quot;title&quot;: &quot;Eloquent JavaScript, Second Edition&quot;,</span>\n<span class=\"grvsc-line\">      &quot;subtitle&quot;: &quot;A Modern Introduction to Programming&quot;,</span>\n<span class=\"grvsc-line\">      &quot;author&quot;: &quot;Marijn Haverbeke&quot;,</span>\n<span class=\"grvsc-line\">      &quot;publisher&quot;: &quot;No Starch Press&quot;,</span>\n<span class=\"grvsc-line\">      &quot;description&quot;: &quot;JavaScript lies at the heart of almost every modern web application, from social apps to the newest browser-based games. Though simple for beginners to pick up and play with, JavaScript is a flexible, complex language that you can use to build full-scale applications.&quot;</span>\n<span class=\"grvsc-line\">    },</span>\n<span class=\"grvsc-line\">    {</span>\n<span class=\"grvsc-line\">      &quot;title&quot;: &quot;Learning JavaScript Design Patterns&quot;,</span>\n<span class=\"grvsc-line\">      &quot;subtitle&quot;: &quot;A JavaScript and jQuery Developer&#39;s Guide&quot;,</span>\n<span class=\"grvsc-line\">      &quot;author&quot;: &quot;Addy Osmani&quot;,</span>\n<span class=\"grvsc-line\">      &quot;publisher&quot;: &quot;O&#39;Reilly Media&quot;,</span>\n<span class=\"grvsc-line\">      &quot;description&quot;: &quot;With Learning JavaScript Design Patterns, you&#39;ll learn how to write beautiful, structured, and maintainable JavaScript by applying classical and modern design patterns to the language. If you want to keep your code efficient, more manageable, and up-to-date with the latest best practices, this book is for you.&quot;</span>\n<span class=\"grvsc-line\">    },</span>\n<span class=\"grvsc-line\">    {</span>\n<span class=\"grvsc-line\">      &quot;title&quot;: &quot;Speaking JavaScript&quot;,</span>\n<span class=\"grvsc-line\">      &quot;subtitle&quot;: &quot;An In-Depth Guide for Programmers&quot;,</span>\n<span class=\"grvsc-line\">      &quot;author&quot;: &quot;Axel Rauschmayer&quot;,</span>\n<span class=\"grvsc-line\">      &quot;publisher&quot;: &quot;O&#39;Reilly Media&quot;,</span>\n<span class=\"grvsc-line\">      &quot;description&quot;: &quot;Like it or not, JavaScript is everywhere these days, from browser to server to mobile and now you, too, need to learn the language or dive deeper than you have. This concise book guides you into and through JavaScript, written by a veteran programmer who once found himself in the same position.&quot;</span>\n<span class=\"grvsc-line\">    },</span>\n<span class=\"grvsc-line\">    {</span>\n<span class=\"grvsc-line\">      &quot;title&quot;: &quot;Programming JavaScript Applications&quot;,</span>\n<span class=\"grvsc-line\">      &quot;subtitle&quot;: &quot;Robust Web Architecture with Node, HTML5, and Modern JS Libraries&quot;,</span>\n<span class=\"grvsc-line\">      &quot;author&quot;: &quot;Eric Elliott&quot;,</span>\n<span class=\"grvsc-line\">      &quot;publisher&quot;: &quot;O&#39;Reilly Media&quot;,</span>\n<span class=\"grvsc-line\">      &quot;description&quot;: &quot;Take advantage of JavaScript&#39;s power to build robust web-scale or enterprise applications that are easy to extend and maintain. By applying the design patterns outlined in this practical book, experienced JavaScript developers will learn how to write flexible and resilient code that&#39;s easier-yes, easier-to work with as your codebase grows.&quot;</span>\n<span class=\"grvsc-line\">    },</span>\n<span class=\"grvsc-line\">    {</span>\n<span class=\"grvsc-line\">      &quot;title&quot;: &quot;Understanding ECMAScript 6&quot;,</span>\n<span class=\"grvsc-line\">      &quot;subtitle&quot;: &quot;The Definitive Guide for JavaScript Developers&quot;,</span>\n<span class=\"grvsc-line\">      &quot;author&quot;: &quot;Nicholas C. Zakas&quot;,</span>\n<span class=\"grvsc-line\">      &quot;publisher&quot;: &quot;No Starch Press&quot;,</span>\n<span class=\"grvsc-line\">      &quot;description&quot;: &quot;ECMAScript 6 represents the biggest update to the core of JavaScript in the history of the language. In Understanding ECMAScript 6, expert developer Nicholas C. Zakas provides a complete guide to the object types, syntax, and other exciting changes that ECMAScript 6 brings to JavaScript.&quot;</span>\n<span class=\"grvsc-line\">    }</span>\n<span class=\"grvsc-line\">])</span></code></pre>\n<h2 id=\"creating-a-text-index\" style=\"position:relative;\"><a href=\"#creating-a-text-index\" aria-label=\"creating a text index permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Creating a Text Index</h2>\n<p>We need to create a text index on the fields to perform the text search. We can create this on single or multiple fields. The following statement will create a text index on a single field.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt;db.books.createIndex({&quot;description&quot;:&quot;text&quot;})</span></code></pre>\n<p>We will create a text index on the <em>description</em> and <em>subtitle</em> fields for this tutorial. We can create only one text index per collection in MongoDB. So We will create a compound text index using the following statement.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt;db.books.createIndex({&quot;subtitle&quot;:&quot;text&quot;,&quot;description&quot;:&quot;text&quot;})</span></code></pre>\n<h2 id=\"search\" style=\"position:relative;\"><a href=\"#search\" aria-label=\"search permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>$search</h2>\n<p>Now we will try to search documents that have the keywords 'ECMAScript' in the <em>description</em> and <em>subtitle</em> fields. For this, we can use the below statement.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">db.books.find({$text: {$search: &quot;ECMAScript&quot;}})</span></code></pre>\n<p><strong>Example</strong></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt;db.books.find({$text: {$search: &quot;ECMAScript&quot;}},{ subtitle: 1, description: 1 })</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">    &quot;_id&quot; : ObjectId(&quot;602b09cb3cb6144ada1c62fe&quot;),</span>\n<span class=\"grvsc-line\">    &quot;subtitle&quot; : &quot;The Definitive Guide for JavaScript Developers&quot;,</span>\n<span class=\"grvsc-line\">    &quot;description&quot; : &quot;ECMAScript 6 represents the biggest update to the core of JavaScript in the history of the language. In Understanding ECMAScript 6, expert developer Nicholas C. Zakas provides a complete guide to the object types, syntax, and other exciting changes that ECMAScript 6 brings to JavaScript.&quot;</span>\n<span class=\"grvsc-line\">\t}</span>\n<span class=\"grvsc-line\">&gt;</span></code></pre>\n<h3 id=\"phrases\" style=\"position:relative;\"><a href=\"#phrases\" aria-label=\"phrases permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Phrases</h3>\n<p>You can search for phrases using the text index. By default, text search performs an OR search for all words in the phrase. If you want to search 'modern design patterns', it will search for documents with the keywords either modern, design, or patterns.</p>\n<p><strong>Example</strong></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"6\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt;db.books.find({$text: {$search: &quot;modern design patterns&quot;}},{ subtitle: 1, description: 1 })</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">    &quot;_id&quot; : ObjectId(&quot;602b098f3cb6144ada1c2ea1&quot;),</span>\n<span class=\"grvsc-line\">    &quot;subtitle&quot; : &quot;A JavaScript and jQuery Developer&#39;s Guide&quot;,</span>\n<span class=\"grvsc-line\">    &quot;description&quot; : &quot;With Learning JavaScript Design Patterns, you&#39;ll learn how to write beautiful, structured, and maintainable JavaScript by applying classical and modern design patterns to the language. If you want to keep your code efficient, more manageable, and up-to-date with the latest best practices, this book is for you.&quot;</span>\n<span class=\"grvsc-line\">\t},</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">    &quot;_id&quot; : ObjectId(&quot;602b09b93cb6144ada1c4bca&quot;),</span>\n<span class=\"grvsc-line\">    &quot;subtitle&quot; : &quot;Robust Web Architecture with Node, HTML5, and Modern JS Libraries&quot;,</span>\n<span class=\"grvsc-line\">    &quot;description&quot; : &quot;Take advantage of JavaScript&#39;s power to build robust web-scale or enterprise applications that are easy to extend and maintain. By applying the design patterns outlined in this practical book, experienced JavaScript developers will learn how to write flexible and resilient code that&#39;s easier-yes, easier-to work with as your code base grows.&quot;,</span>\n<span class=\"grvsc-line\">\t},</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">    &quot;_id&quot; : ObjectId(&quot;602b095c3cb6144ada1c1028&quot;),</span>\n<span class=\"grvsc-line\">    &quot;subtitle&quot; : &quot;A Modern Introduction to Programming&quot;,</span>\n<span class=\"grvsc-line\">    &quot;description&quot; : &quot;JavaScript lies at the heart of almost every modern web application, from social apps to the newest browser-based games. Though simple for beginners to pick up and play with, JavaScript is a flexible, complex language that you can use to build full-scale applications.&quot;</span>\n<span class=\"grvsc-line\">\t}</span>\n<span class=\"grvsc-line\">&gt;</span></code></pre>\n<p>If you want to search for exact phrases like documents with 'modern design patterns' together, you can do so by specifying double quotes in the search text.</p>\n<p><strong>Example</strong></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"7\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt;db.books.find({$text: {$search: &quot;\\&quot;modern design patterns\\&quot;&quot;}},{ subtitle: 1, description: 1 })</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">    &quot;_id&quot; : ObjectId(&quot;602b098f3cb6144ada1c2ea1&quot;),</span>\n<span class=\"grvsc-line\">    &quot;subtitle&quot; : &quot;A JavaScript and jQuery Developer&#39;s Guide&quot;,</span>\n<span class=\"grvsc-line\">    &quot;description&quot; : &quot;With Learning JavaScript Design Patterns, you&#39;ll learn how to write beautiful, structured, and maintainable JavaScript by applying classical and modern design patterns to the language. If you want to keep your code efficient, more manageable, and up-to-date with the latest best practices, this book is for you.&quot;</span>\n<span class=\"grvsc-line\">}</span></code></pre>\n<h3 id=\"negations\" style=\"position:relative;\"><a href=\"#negations\" aria-label=\"negations permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Negations</h3>\n<p>If you want to exclude the documents containing a particular word, you can use a negation search. For example if you're going to search all documents with the 'JavaScript' but not 'HTML5' or 'ECMAScript', you can search as the below example.</p>\n<p><strong>Example</strong></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"8\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt;db.books.find({$text: {$search: &quot;JavaScript -HTML5 -ECMAScript&quot;}},{ subtitle: 1, description: 1 })</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">    &quot;_id&quot; : ObjectId(&quot;602b098f3cb6144ada1c2ea1&quot;),</span>\n<span class=\"grvsc-line\">    &quot;subtitle&quot; : &quot;A JavaScript and jQuery Developer&#39;s Guide&quot;,</span>\n<span class=\"grvsc-line\">    &quot;description&quot; : &quot;With Learning JavaScript Design Patterns, you&#39;ll learn how to write beautiful, structured, and maintainable JavaScript by applying classical and modern design patterns to the language. If you want to keep your code efficient, more manageable, and up-to-date with the latest best practices, this book is for you.&quot;</span>\n<span class=\"grvsc-line\">\t},</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">    &quot;_id&quot; : ObjectId(&quot;602b09a83cb6144ada1c4973&quot;),</span>\n<span class=\"grvsc-line\">    &quot;subtitle&quot; : &quot;An In-Depth Guide for Programmers&quot;,</span>\n<span class=\"grvsc-line\">    &quot;description&quot; : &quot;Like it or not, JavaScript is everywhere these days, from browser to server to mobile and now you, too, need to learn the language or dive deeper than you have. This concise book guides you into and through JavaScript, written by a veteran programmer who once found himself in the same position.&quot;</span>\n<span class=\"grvsc-line\">\t},</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">    &quot;_id&quot; : ObjectId(&quot;602b095c3cb6144ada1c1028&quot;),</span>\n<span class=\"grvsc-line\">    &quot;subtitle&quot; : &quot;A Modern Introduction to Programming&quot;,</span>\n<span class=\"grvsc-line\">    &quot;description&quot; : &quot;JavaScript lies at the heart of almost every modern web application, from social apps to the newest browser-based games. Though simple for beginners to pick up and play with, JavaScript is a flexible, complex language that you can use to build full-scale applications.&quot;</span>\n<span class=\"grvsc-line\">\t}</span></code></pre>\n<h3 id=\"text-search-score\" style=\"position:relative;\"><a href=\"#text-search-score\" aria-label=\"text search score permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Text Search Score</h3>\n<p>The text search provides a score to each document representing the relevancy of the document with the search query. This score can be used to sort all the records returned in the search result. A higher score will indicate a most relevant match.</p>\n<p><strong>Example</strong></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"9\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt;db.books.find({$text: {$search: &quot;JavaScript &quot;}},{score: {$meta: &quot;textScore&quot;}, subtitle: 1, description: 1 }).sort({score:{$meta:&quot;textScore&quot;}})</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">    &quot;_id&quot; : ObjectId(&quot;602b098f3cb6144ada1c2ea1&quot;),</span>\n<span class=\"grvsc-line\">    &quot;subtitle&quot; : &quot;A JavaScript and jQuery Developer&#39;s Guide&quot;,</span>\n<span class=\"grvsc-line\">    &quot;description&quot; : &quot;With Learning JavaScript Design Patterns, you&#39;ll learn how to write beautiful, structured, and maintainable JavaScript by applying classical and modern design patterns to the language. If you want to keep your code efficient, more manageable, and up-to-date with the latest best practices, this book is for you.&quot;,</span>\n<span class=\"grvsc-line\">    &quot;score&quot; : 1.43269230769231</span>\n<span class=\"grvsc-line\">\t},</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">    &quot;_id&quot; : ObjectId(&quot;602b09cb3cb6144ada1c62fe&quot;),</span>\n<span class=\"grvsc-line\">    &quot;subtitle&quot; : &quot;The Definitive Guide for JavaScript Developers&quot;,</span>\n<span class=\"grvsc-line\">    &quot;description&quot; : &quot;ECMAScript 6 represents the biggest update to the core of JavaScript in the history of the language. In Understanding ECMAScript 6, expert developer Nicholas C. Zakas provides a complete guide to the object types, syntax, and other exciting changes that ECMAScript 6 brings to JavaScript.&quot;,</span>\n<span class=\"grvsc-line\">    &quot;score&quot; : 1.42672413793103</span>\n<span class=\"grvsc-line\">\t},</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">    &quot;_id&quot; : ObjectId(&quot;602b09a83cb6144ada1c4973&quot;),</span>\n<span class=\"grvsc-line\">    &quot;subtitle&quot; : &quot;An In-Depth Guide for Programmers&quot;,</span>\n<span class=\"grvsc-line\">    &quot;description&quot; : &quot;Like it or not, JavaScript is everywhere these days, from browser to server to mobile and now you, too, need to learn the language or dive deeper than you have. This concise book guides you into and through JavaScript, written by a veteran programmer who once found himself in the same position.&quot;,</span>\n<span class=\"grvsc-line\">    &quot;score&quot; : 0.818181818181818</span>\n<span class=\"grvsc-line\">\t},</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">    &quot;_id&quot; : ObjectId(&quot;602b095c3cb6144ada1c1028&quot;),</span>\n<span class=\"grvsc-line\">    &quot;subtitle&quot; : &quot;A Modern Introduction to Programming&quot;,</span>\n<span class=\"grvsc-line\">    &quot;description&quot; : &quot;JavaScript lies at the heart of almost every modern web application, from social apps to the newest browser-based games. Though simple for beginners to pick up and play with, JavaScript is a flexible, complex language that you can use to build full-scale applications.&quot;,</span>\n<span class=\"grvsc-line\">    &quot;score&quot; : 0.801724137931034</span>\n<span class=\"grvsc-line\">\t},</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">    &quot;_id&quot; : ObjectId(&quot;602b09b93cb6144ada1c4bca&quot;),</span>\n<span class=\"grvsc-line\">    &quot;subtitle&quot; : &quot;Robust Web Architecture with Node, HTML5, and Modern JS Libraries&quot;,</span>\n<span class=\"grvsc-line\">    &quot;description&quot; : &quot;Take advantage of JavaScript&#39;s power to build robust web-scale or enterprise applications that are easy to extend and maintain. By applying the design patterns outlined in this practical book, experienced JavaScript developers will learn how to write flexible and resilient code that&#39;s easier-yes, easier-to work with as your codebase grows.&quot;,</span>\n<span class=\"grvsc-line\">    &quot;score&quot; : 0.792857142857143</span>\n<span class=\"grvsc-line\">\t}</span></code></pre>\n<h3 id=\"stop-words\" style=\"position:relative;\"><a href=\"#stop-words\" aria-label=\"stop words permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Stop Words</h3>\n<p>The $text operator filters out the language-specific stop words, such as a, an, the and in English. The below search will not return any document in the result.</p>\n<p><strong>Example</strong></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"10\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt;db.books.find({$text: {$search: &quot;is&quot;}},{subtitle: 1, description: 1 })</span>\n<span class=\"grvsc-line\">\tFetched 0 record(s)</span></code></pre>\n<h3 id=\"stemmed-words\" style=\"position:relative;\"><a href=\"#stemmed-words\" aria-label=\"stemmed words permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Stemmed Words</h3>\n<p>The $text operator matches on the complete stemmed word. So if some document field contains the word learning or learn, a search on the term learning or learn would result in the same.</p>\n<p><strong>Example</strong></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"11\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt;db.books.find({$text: {$search: &quot; learn&quot;}},{subtitle: 1, description: 1 }) or &gt;db.books.find({$text: {$search: &quot; learning&quot;}},{subtitle: 1, description: 1 })</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">    &quot;_id&quot; : ObjectId(&quot;602b098f3cb6144ada1c2ea1&quot;),</span>\n<span class=\"grvsc-line\">    &quot;subtitle&quot; : &quot;A JavaScript and jQuery Developer&#39;s Guide&quot;,</span>\n<span class=\"grvsc-line\">    &quot;description&quot; : &quot;With Learning JavaScript Design Patterns, you&#39;ll learn how to write beautiful, structured, and maintainable JavaScript by applying classical and modern design patterns to the language. If you want to keep your code efficient, more manageable, and up-to-date with the latest best practices, this book is for you.&quot;</span>\n<span class=\"grvsc-line\">\t},</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">    &quot;_id&quot; : ObjectId(&quot;602b09a83cb6144ada1c4973&quot;),</span>\n<span class=\"grvsc-line\">    &quot;subtitle&quot; : &quot;An In-Depth Guide for Programmers&quot;,</span>\n<span class=\"grvsc-line\">    &quot;description&quot; : &quot;Like it or not, JavaScript is everywhere these days, from browser to server to mobile and now you, too, need to learn the language or dive deeper than you have. This concise book guides you into and through JavaScript, written by a veteran programmer who once found himself in the same position.&quot;</span>\n<span class=\"grvsc-line\">\t},</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">    &quot;_id&quot; : ObjectId(&quot;602b09b93cb6144ada1c4bca&quot;),</span>\n<span class=\"grvsc-line\">    &quot;subtitle&quot; : &quot;Robust Web Architecture with Node, HTML5, and Modern JS Libraries&quot;,</span>\n<span class=\"grvsc-line\">    &quot;description&quot; : &quot;Take advantage of JavaScript&#39;s power to build robust web-scale or enterprise applications that are easy to extend and maintain. By applying the design patterns outlined in this practical book, experienced JavaScript developers will learn how to write flexible and resilient code that&#39;s easier-yes, easier-to work with as your codebase grows.&quot;</span>\n<span class=\"grvsc-line\">\t}</span></code></pre>\n<h2 id=\"conclusion\" style=\"position:relative;\"><a href=\"#conclusion\" aria-label=\"conclusion permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Conclusion</h2>\n<p>I hope you learned something new today. Here is an interesting article on <a href=\"https://www.loginradius.com/blog/engineering/self-hosted-mongo/\">Self-Hosted MongoDB</a>. I also invite you to try stuff on your own and share your experience in the comment section. Furthermore, if you face any problems with any of the above definitions, please feel free to ask me in the comments section below.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n</style>","headings":[{"value":"Create a Sample Database","depth":2},{"value":"Creating a Text Index","depth":2},{"value":"$search","depth":2},{"value":"Phrases","depth":3},{"value":"Negations","depth":3},{"value":"Text Search Score","depth":3},{"value":"Stop Words","depth":3},{"value":"Stemmed Words","depth":3},{"value":"Conclusion","depth":2}],"fields":{"slug":"/engineering/full-text-search-in-mongodb/"},"frontmatter":{"metatitle":null,"metadescription":null,"description":"MongoDB full text search tutorial. In this blog, we will learn how to perform a full-text search in MongoDB using text index.","title":"How to do Full-Text Search in MongoDB","canonical":null,"date":"February 16, 2021","updated_date":null,"tags":["MongoDB"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.5037593984962405,"src":"/static/4baf4f017625700154de23531c7ef2a8/03979/coverImage.png","srcSet":"/static/4baf4f017625700154de23531c7ef2a8/f5f11/coverImage.png 200w,\n/static/4baf4f017625700154de23531c7ef2a8/6d133/coverImage.png 400w,\n/static/4baf4f017625700154de23531c7ef2a8/03979/coverImage.png 800w,\n/static/4baf4f017625700154de23531c7ef2a8/aca38/coverImage.png 1200w,\n/static/4baf4f017625700154de23531c7ef2a8/3741a/coverImage.png 1600w,\n/static/4baf4f017625700154de23531c7ef2a8/ca599/coverImage.png 1920w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Anil Gupta","github":"anilswm","bio":null,"avatar":null}}}},"pageContext":{"id":"c6611215-4458-5540-a382-1ff1548299c9","fields__slug":"/engineering/full-text-search-in-mongodb/","__params":{"fields__slug":"engineering"}}},"staticQueryHashes":["1171199041","1384082988","1711371485","1753898100","2100481360","229320306","23180105","528864852"]}