{"componentChunkName":"component---src-pages-author-author-yaml-id-js","path":"/author/anil-gupta/","result":{"data":{"allMarkdownRemark":{"edges":[{"node":{"id":"c6611215-4458-5540-a382-1ff1548299c9","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>","frontmatter":{"title":"How to do Full-Text Search in MongoDB","author":{"id":"Anil Gupta","github":"anilswm","avatar":null},"date":"February 16, 2021","updated_date":null,"tags":["MongoDB"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.5037593984962405,"src":"/static/f4c86ce656fbbcd1cebd60b0c6606d53/58556/coverImage.webp","srcSet":"/static/f4c86ce656fbbcd1cebd60b0c6606d53/61e93/coverImage.webp 200w,\n/static/f4c86ce656fbbcd1cebd60b0c6606d53/1f5c5/coverImage.webp 400w,\n/static/f4c86ce656fbbcd1cebd60b0c6606d53/58556/coverImage.webp 800w,\n/static/f4c86ce656fbbcd1cebd60b0c6606d53/99238/coverImage.webp 1200w,\n/static/f4c86ce656fbbcd1cebd60b0c6606d53/7c22d/coverImage.webp 1600w,\n/static/f4c86ce656fbbcd1cebd60b0c6606d53/25f09/coverImage.webp 1920w","sizes":"(max-width: 800px) 100vw, 800px"}}}},"fields":{"authorId":"Anil Gupta","slug":"/engineering/full-text-search-in-mongodb/"}}},{"node":{"id":"29ac238e-b3d9-5f78-83c0-ec97abe366ad","html":"<p>In this article, we will address how to perform basic query operations in MongoDB. We are producing data at an unparalleled pace now following the global spread of the internet. Since it will require us to collect/request the required data from the database to conduct some kind of analysis, it is of utmost importance that we choose the right tool to query the data.</p>\n<p>This is where MongoDB comes in, specifically. MongoDB is an unstructured database which, in the form of documents, stores data. In addition, MongoDB is very effective in <a href=\"https://www.loginradius.com/blog/engineering/live-data-migration-mongodb/\">handling enormous amounts of data</a> and is the most commonly used NoSQL database as it provides rich query language and versatile and easy data access.</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 the start, we will create a sample DB with some sample data to perform all operations.</p>\n<p>We will create a database with name <em>myDB</em> and will create a collection with name <em>orders</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;orders&quot;)</span>\n<span class=\"grvsc-line\">&gt;</span></code></pre>\n<p><em>MongoDB doesn't use the rows and columns. It stores the data in a document format. A collection is a group of documents.</em></p>\n<p>You can check all collections in a database 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; use myDB</span>\n<span class=\"grvsc-line\">&gt;show collections</span>\n<span class=\"grvsc-line\">orders</span>\n<span class=\"grvsc-line\">system.indexes</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=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt; db.orders.insert([</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">\t\tCustomer: &quot;abc&quot;,</span>\n<span class=\"grvsc-line\">\t\tAddress:{&quot;City&quot;:&quot;Jaipur&quot;,&quot;Country&quot;:&quot;India&quot;},</span>\n<span class=\"grvsc-line\">\t\tPaymentMode&quot;:&quot;Card&quot;,</span>\n<span class=\"grvsc-line\">\t\tEmail:&quot;abc@mail.in&quot;,</span>\n<span class=\"grvsc-line\">\t\tOrderTotal: 1000.00,</span>\n<span class=\"grvsc-line\">\t\tOrderItems:[</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;notebook&quot;,&quot;Price&quot;:&quot;150.00&quot;,&quot;Qty&quot;:10},</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;paper&quot;,&quot;Price&quot;:&quot;10.00&quot;,&quot;Qty&quot;:5},</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;journal&quot;,&quot;Price&quot;:&quot;200.00&quot;,&quot;Qty&quot;:2},</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;postcard&quot;,&quot;Price&quot;:&quot;10.00&quot;,&quot;Qty&quot;:500}</span>\n<span class=\"grvsc-line\">\t\t]\t\t</span>\n<span class=\"grvsc-line\">\t},</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">\t\tCustomer: &quot;xyz&quot;,</span>\n<span class=\"grvsc-line\">\t\tAddress:{&quot;City&quot;:&quot;Delhi&quot;,&quot;Country&quot;:&quot;India&quot;},</span>\n<span class=\"grvsc-line\">\t\tPaymentMode&quot;:&quot;Cash&quot;,</span>\n<span class=\"grvsc-line\">\t\tOrderTotal: 800.00,</span>\n<span class=\"grvsc-line\">\t\tOrderItems:[</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;notebook&quot;,&quot;Price&quot;:&quot;150.00&quot;,&quot;Qty&quot;:5},</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;paper&quot;,&quot;Price&quot;:&quot;10.00&quot;,&quot;Qty&quot;:5},</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;postcard&quot;,&quot;Price&quot;:&quot;10.00&quot;,&quot;Qty&quot;:500}</span>\n<span class=\"grvsc-line\">\t\t]\t\t</span>\n<span class=\"grvsc-line\">\t},</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">\t\tCustomer: &quot;ron&quot;,</span>\n<span class=\"grvsc-line\">\t\tAddress:{&quot;City&quot;:&quot;New York&quot;,&quot;Country&quot;:&quot;USA&quot;},</span>\n<span class=\"grvsc-line\">\t\tPaymentMode&quot;:&quot;Card&quot;,</span>\n<span class=\"grvsc-line\">\t\tEmail:&quot;ron@mail.com&quot;,</span>\n<span class=\"grvsc-line\">\t\tOrderTotal: 800.00,</span>\n<span class=\"grvsc-line\">\t\tOrderItems:[</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;notebook&quot;,&quot;Price&quot;:&quot;150.00&quot;,&quot;Qty&quot;:5},</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;postcard&quot;,&quot;Price&quot;:&quot;10.00&quot;,&quot;Qty&quot;:00}</span>\n<span class=\"grvsc-line\">\t\t]\t\t</span>\n<span class=\"grvsc-line\">\t}</span>\n<span class=\"grvsc-line\">])</span></code></pre>\n<p><em>A document is the equivalent of an RDBMS row. It doesn't need to have the same schema in each document. It means a document might not have any field that doesn't have any value.</em></p>\n<h2 id=\"query-documents\" style=\"position:relative;\"><a href=\"#query-documents\" aria-label=\"query documents 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>Query Documents</h2>\n<h3 id=\"find-method\" style=\"position:relative;\"><a href=\"#find-method\" aria-label=\"find method 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>find() method</h3>\n<p>You need to use the find() method to query documents from MongoDB collections. The following statement will retrieve all documents from the collection.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt; db.orders.find()</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">\t\t&quot;_id&quot; : ObjectId(&quot;5dd4e2cc0821d3b44607534c&quot;)</span>\n<span class=\"grvsc-line\">\t\tCustomer: &quot;abc&quot;,</span>\n<span class=\"grvsc-line\">\t\tAddress:{&quot;City&quot;:&quot;Jaipur&quot;,&quot;Country&quot;:&quot;India&quot;},</span>\n<span class=\"grvsc-line\">\t\tPaymentMode&quot;:&quot;Card&quot;,</span>\n<span class=\"grvsc-line\">\t\tEmail:&quot;abc@mail.com&quot;,</span>\n<span class=\"grvsc-line\">\t\tOrderTotal: 1000.00,</span>\n<span class=\"grvsc-line\">\t\tOrderItems:[</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;notebook&quot;,&quot;Price&quot;:&quot;150.00&quot;,&quot;Qty&quot;:10},</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;paper&quot;,&quot;Price&quot;:&quot;10.00&quot;,&quot;Qty&quot;:5},</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;journal&quot;,&quot;Price&quot;:&quot;200.00&quot;,&quot;Qty&quot;:2},</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;postcard&quot;,&quot;Price&quot;:&quot;10.00&quot;,&quot;Qty&quot;:500}</span>\n<span class=\"grvsc-line\">\t\t]\t\t</span>\n<span class=\"grvsc-line\">\t},</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">\t\t&quot;_id&quot; : ObjectId(&quot;5dd4e2cc0821d3b44607544c&quot;),</span>\n<span class=\"grvsc-line\">\t\tCustomer: &quot;xyz&quot;,</span>\n<span class=\"grvsc-line\">\t\tAddress:{&quot;City&quot;:&quot;Delhi&quot;,&quot;Country&quot;:&quot;India&quot;},</span>\n<span class=\"grvsc-line\">\t\tPaymentMode&quot;:&quot;Cash&quot;,</span>\n<span class=\"grvsc-line\">\t\tOrderTotal: 800.00,</span>\n<span class=\"grvsc-line\">\t\tOrderItems:[</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;notebook&quot;,&quot;Price&quot;:&quot;150.00&quot;,&quot;Qty&quot;:5},</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;paper&quot;,&quot;Price&quot;:&quot;10.00&quot;,&quot;Qty&quot;:5},</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;postcard&quot;,&quot;Price&quot;:&quot;10.00&quot;,&quot;Qty&quot;:500}</span>\n<span class=\"grvsc-line\">\t\t]</span>\n<span class=\"grvsc-line\">\t},</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">\t\t&quot;_id&quot; : ObjectId(&quot;5dd4e2cc0821d3b44607644c&quot;),</span>\n<span class=\"grvsc-line\">\t\tCustomer: &quot;ron&quot;,</span>\n<span class=\"grvsc-line\">\t\tAddress:{&quot;City&quot;:&quot;New York&quot;,&quot;Country&quot;:&quot;USA&quot;},</span>\n<span class=\"grvsc-line\">\t\tPaymentMode&quot;:&quot;Card&quot;,</span>\n<span class=\"grvsc-line\">\t\tEmail:&quot;ron@mail.com&quot;,</span>\n<span class=\"grvsc-line\">\t\tOrderTotal: 600.00,</span>\n<span class=\"grvsc-line\">\t\tOrderItems:[</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;notebook&quot;,&quot;Price&quot;:&quot;150.00&quot;,&quot;Qty&quot;:5},</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;postcard&quot;,&quot;Price&quot;:&quot;10.00&quot;,&quot;Qty&quot;:00}</span>\n<span class=\"grvsc-line\">\t\t]</span>\n<span class=\"grvsc-line\">\t}</span>\n<span class=\"grvsc-line\">&gt;</span></code></pre>\n<h3 id=\"projection\" style=\"position:relative;\"><a href=\"#projection\" aria-label=\"projection 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>Projection</h3>\n<p>If you want to fetch only selected fields then you can use the projection. Following statement will fetch only <em>Customer</em> and <em>Email</em> field.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt; db.orders.find( { }, { Customer: 1, Email: 1 })</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">\t\t&quot;_id&quot; : ObjectId(&quot;5dd4e2cc0821d3b44607534c&quot;)</span>\n<span class=\"grvsc-line\">\t\tCustomer: &quot;abc&quot;,</span>\n<span class=\"grvsc-line\">\t\tEmail:&quot;abc@mail.com&quot;</span>\n<span class=\"grvsc-line\">\t},</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">\t\t&quot;_id&quot; : ObjectId(&quot;5dd4e2cc0821d3b44607544c&quot;),</span>\n<span class=\"grvsc-line\">\t\tCustomer: &quot;xyz&quot;\t\t</span>\n<span class=\"grvsc-line\">\t},</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">\t\t&quot;_id&quot; : ObjectId(&quot;5dd4e2cc0821d3b44607644c&quot;),</span>\n<span class=\"grvsc-line\">\t\tCustomer: &quot;ron&quot;,</span>\n<span class=\"grvsc-line\">\t\tEmail:&quot;ron@mail.com&quot;\t\t</span>\n<span class=\"grvsc-line\">\t}</span>\n<span class=\"grvsc-line\">&gt;</span></code></pre>\n<h3 id=\"filter-the-documents-by-specifying-a-condition\" style=\"position:relative;\"><a href=\"#filter-the-documents-by-specifying-a-condition\" aria-label=\"filter the documents by specifying a condition 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>Filter the Documents by Specifying a Condition</h3>\n<p>Now we will learn how we can fetch the documents that match a specified condition. MongoDB provides many comparison operators for this.</p>\n<h4 id=\"1-eq-operator\" style=\"position:relative;\"><a href=\"#1-eq-operator\" aria-label=\"1 eq operator 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>1. $eq Operator</h4>\n<p>The $eq operator checks the equality of the field value with the specified value. To fetch the order where <em>PaymentMode</em> is 'Card' you can use the following statement</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt;db.orders.find( { PaymentMode: { $eq: &quot;Card&quot; } } )</span></code></pre>\n<p><em>This query can be written also like below</em></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"6\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt;db.orders.find( { PaymentMode: &quot;Card&quot; } )</span></code></pre>\n<p><em>A similar SQL statement would be as follows</em></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"7\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">SELECT * FROM orders WHERE PaymentMode=&quot;Card&quot;</span></code></pre>\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.orders.find( { PaymentMode: &quot;Card&quot; }, { Customer: 1, PaymentMode: 1 } )</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">\t\t&quot;_id&quot; : ObjectId(&quot;5dd4e2cc0821d3b44607534c&quot;)</span>\n<span class=\"grvsc-line\">\t\tCustomer: &quot;abc&quot;,</span>\n<span class=\"grvsc-line\">\t\tPaymentMode&quot;:&quot;Card&quot;\t\t\t\t</span>\n<span class=\"grvsc-line\">\t},</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">\t\t&quot;_id&quot; : ObjectId(&quot;5dd4e2cc0821d3b44607644c&quot;),</span>\n<span class=\"grvsc-line\">\t\tCustomer: &quot;ron&quot;,</span>\n<span class=\"grvsc-line\">\t\tPaymentMode&quot;:&quot;Card&quot;</span>\n<span class=\"grvsc-line\">\t}</span>\n<span class=\"grvsc-line\">&gt;</span></code></pre>\n<p><strong>$eq Operator with embedded document</strong></p>\n<p>You may have noticed that we inserted an embedded document <em>Address</em> in the <em>Orders</em> collection. If you want to fetch the order where <em>Country</em> is 'India' you can use a dot notation like the following statement.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"9\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt;db.Orders.find( { &quot;Address.Country&quot;: { $eq: &quot;India&quot; } } )</span></code></pre>\n<p><em>This query can be written also like below</em></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"10\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt;db.Orders.find( { &quot;Address.Country&quot;:&quot;India&quot; } )</span></code></pre>\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.Orders.find( { &quot;Address.Country&quot;: { $eq: &quot;India&quot; } } , { Customer: 1, Address: 1 })</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">\t\t&quot;_id&quot; : ObjectId(&quot;5dd4e2cc0821d3b44607534c&quot;)</span>\n<span class=\"grvsc-line\">\t\tCustomer: &quot;abc&quot;,</span>\n<span class=\"grvsc-line\">\t\tAddress:{&quot;City&quot;:&quot;Jaipur&quot;,&quot;Country&quot;:&quot;India&quot;}</span>\n<span class=\"grvsc-line\">\t},</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">\t\t&quot;_id&quot; : ObjectId(&quot;5dd4e2cc0821d3b44607544c&quot;),</span>\n<span class=\"grvsc-line\">\t\tCustomer: &quot;xyz&quot;,</span>\n<span class=\"grvsc-line\">\t\tAddress:{&quot;City&quot;:&quot;Delhi&quot;,&quot;Country&quot;:&quot;India&quot;}</span>\n<span class=\"grvsc-line\">\t}</span>\n<span class=\"grvsc-line\">&gt;</span></code></pre>\n<p><strong>$eq Operator with array</strong></p>\n<p>$eq operator will retrieve all the documents if the specified condition is true for any item in an array. We have an <em>OrderItems</em> array in the document. If you want to filter the documents where 'paper' were also ordered then the statement would be as follows.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"12\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt;db.Orders.find( { &quot;OrderItems.ItemName&quot;: { $eq: &quot;paper&quot; } } )</span></code></pre>\n<p><em>This query can be written also like below</em></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"13\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt;db.Orders.find( { &quot;OrderItems.ItemName&quot;: &quot;paper&quot;  } )</span></code></pre>\n<p><strong>Example</strong></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"14\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt;db.Orders.find( { &quot;OrderItems.ItemName&quot;: { $eq: &quot;paper&quot; } } , { Customer: 1, OrderItems: 1 })</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">\t\t&quot;_id&quot; : ObjectId(&quot;5dd4e2cc0821d3b44607534c&quot;)</span>\n<span class=\"grvsc-line\">\t\tCustomer: &quot;abc&quot;,</span>\n<span class=\"grvsc-line\">\t\tOrderItems:[</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;notebook&quot;,&quot;Price&quot;:&quot;150.00&quot;,&quot;Qty&quot;:10},</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;paper&quot;,&quot;Price&quot;:&quot;10.00&quot;,&quot;Qty&quot;:5},</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;journal&quot;,&quot;Price&quot;:&quot;200.00&quot;,&quot;Qty&quot;:2},</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;postcard&quot;,&quot;Price&quot;:&quot;10.00&quot;,&quot;Qty&quot;:500}</span>\n<span class=\"grvsc-line\">\t\t]\t\t</span>\n<span class=\"grvsc-line\">\t},</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">\t\t&quot;_id&quot; : ObjectId(&quot;5dd4e2cc0821d3b44607544c&quot;),</span>\n<span class=\"grvsc-line\">\t\tCustomer: &quot;xyz&quot;,</span>\n<span class=\"grvsc-line\">\t\tOrderItems:[</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;notebook&quot;,&quot;Price&quot;:&quot;150.00&quot;,&quot;Qty&quot;:5},</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;paper&quot;,&quot;Price&quot;:&quot;10.00&quot;,&quot;Qty&quot;:5},</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;postcard&quot;,&quot;Price&quot;:&quot;10.00&quot;,&quot;Qty&quot;:500}</span>\n<span class=\"grvsc-line\">\t\t]</span>\n<span class=\"grvsc-line\">\t}</span>\n<span class=\"grvsc-line\">&gt;</span></code></pre>\n<h4 id=\"2-gt-operator\" style=\"position:relative;\"><a href=\"#2-gt-operator\" aria-label=\"2 gt operator 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>2. $gt Operator</h4>\n<p>You can use the $gt operator to retrieve the documents where a field’s value is greater than the specified value. The following statement will fetch the documents where <em>OrderTotal</em> is greater than 800.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"15\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt;db.orders.find( { OrderTotal: { $gt: 800.00 } } )</span></code></pre>\n<p><em>A similar SQL statement would be as follows</em></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"16\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">SELECT * FROM orders WHERE OrderTotal&gt;800.00</span></code></pre>\n<p><strong>Example</strong></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"17\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt;db.Orders.find( { &quot;OrderTotal&quot;: { $gt: 800.00 } } , { Customer: 1, OrderTotal: 1 })</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">\t\t&quot;_id&quot; : ObjectId(&quot;5dd4e2cc0821d3b44607534c&quot;)</span>\n<span class=\"grvsc-line\">\t\tCustomer: &quot;abc&quot;,</span>\n<span class=\"grvsc-line\">\t\tOrderTotal: 1000.00</span>\n<span class=\"grvsc-line\">\t}</span>\n<span class=\"grvsc-line\">&gt;</span></code></pre>\n<h4 id=\"3-gte-operator\" style=\"position:relative;\"><a href=\"#3-gte-operator\" aria-label=\"3 gte operator 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>3. $gte Operator</h4>\n<p>You can use the $gte operator to retrieve the documents where a field’s value is greater than or equal to the specified value. The following statement will fetch the documents where <em>OrderTotal</em> is greater than or equal to 800.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"18\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt;db.orders.find( { OrderTotal: { $gte: 800.00 } } )</span></code></pre>\n<p><em>A similar SQL statement would be as follows</em></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"19\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">SELECT * FROM orders WHERE OrderTotal&gt;=800.00</span></code></pre>\n<p><strong>Example</strong></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"20\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt;db.Orders.find( { &quot;OrderTotal&quot;: { $gte: 800.00 } } , { Customer: 1, OrderTotal: 1 })</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">\t\t&quot;_id&quot; : ObjectId(&quot;5dd4e2cc0821d3b44607534c&quot;)</span>\n<span class=\"grvsc-line\">\t\tCustomer: &quot;abc&quot;,</span>\n<span class=\"grvsc-line\">\t\tOrderTotal: 1000.00</span>\n<span class=\"grvsc-line\">\t},</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">\t\t&quot;_id&quot; : ObjectId(&quot;5dd4e2cc0821d3b44607544c&quot;),</span>\n<span class=\"grvsc-line\">\t\tCustomer: &quot;xyz&quot;,</span>\n<span class=\"grvsc-line\">\t\tOrderTotal: 800.00</span>\n<span class=\"grvsc-line\">\t}</span>\n<span class=\"grvsc-line\">&gt;</span></code></pre>\n<h4 id=\"4-lt-operator\" style=\"position:relative;\"><a href=\"#4-lt-operator\" aria-label=\"4 lt operator 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>4. $lt Operator</h4>\n<p>You can use the $lt operator to retrieve the documents where a field’s value is less than the specified value. The following statement will fetch the documents where <em>OrderTotal</em> is less than 800.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"21\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt;db.orders.find( { OrderTotal: { $lt: 800.00 } } )</span></code></pre>\n<p><em>A similar SQL statement would be as follows</em></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"22\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">SELECT * FROM orders WHERE OrderTotal&lt;800.00</span></code></pre>\n<p><strong>Example</strong></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"23\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt;db.Orders.find( { &quot;OrderTotal&quot;: { $lt: 800.00 } } , { Customer: 1, OrderTotal: 1 })</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">\t\t&quot;_id&quot; : ObjectId(&quot;5dd4e2cc0821d3b44607644c&quot;),</span>\n<span class=\"grvsc-line\">\t\tCustomer: &quot;ron&quot;,</span>\n<span class=\"grvsc-line\">\t\tOrderTotal: 600.00</span>\n<span class=\"grvsc-line\">\t}</span>\n<span class=\"grvsc-line\">&gt;</span></code></pre>\n<h4 id=\"4-lte-operator\" style=\"position:relative;\"><a href=\"#4-lte-operator\" aria-label=\"4 lte operator 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>4. $lte Operator</h4>\n<p>You can use the $lte operator to retrieve the documents where a field’s value is less than or equal to the specified value. Following statement will fetch the documents where <em>OrderTotal</em> is less than or equal to 800.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"24\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt;db.orders.find( { OrderTotal: { $lte: 800.00 } } )</span></code></pre>\n<p><em>A similar SQL statement would be as follows</em></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"25\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">SELECT * FROM orders WHERE OrderTotal&lt;=800.00</span></code></pre>\n<p><strong>Example</strong></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"26\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt;db.Orders.find( { &quot;OrderTotal&quot;: { $lte: 800.00 } } , { Customer: 1, OrderTotal: 1 })</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">\t\t&quot;_id&quot; : ObjectId(&quot;5dd4e2cc0821d3b44607544c&quot;),</span>\n<span class=\"grvsc-line\">\t\tCustomer: &quot;xyz&quot;,</span>\n<span class=\"grvsc-line\">\t\tOrderTotal: 800.00</span>\n<span class=\"grvsc-line\">\t},</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">\t\t&quot;_id&quot; : ObjectId(&quot;5dd4e2cc0821d3b44607644c&quot;),</span>\n<span class=\"grvsc-line\">\t\tCustomer: &quot;ron&quot;,</span>\n<span class=\"grvsc-line\">\t\tOrderTotal: 600.00</span>\n<span class=\"grvsc-line\">\t}</span>\n<span class=\"grvsc-line\">&gt;</span></code></pre>\n<h4 id=\"5-ne-operator\" style=\"position:relative;\"><a href=\"#5-ne-operator\" aria-label=\"5 ne operator 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>5. $ne Operator</h4>\n<p>You can use the $ne operator to retrieve the documents where a field’s value is not equal to the specified value.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"27\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt;db.orders.find( { PaymentMode: { $ne: &quot;Card&quot; } } )</span></code></pre>\n<p><em>A similar SQL statement would be as follows</em></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"28\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">SELECT * FROM orders WHERE PaymentMode != &quot;Card&quot;</span></code></pre>\n<p><strong>Example</strong></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"29\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt;db.Orders.find( { &quot;PaymentMode&quot;: { $ne: &quot;Card&quot; } } , { Customer: 1, PaymentMode: 1 })</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">\t\t&quot;_id&quot; : ObjectId(&quot;5dd4e2cc0821d3b44607544c&quot;),</span>\n<span class=\"grvsc-line\">\t\tCustomer: &quot;xyz&quot;,</span>\n<span class=\"grvsc-line\">\t\tPaymentMode&quot;:&quot;Cash&quot;</span>\n<span class=\"grvsc-line\">\t}</span>\n<span class=\"grvsc-line\">&gt;</span></code></pre>\n<h4 id=\"6-in-operator\" style=\"position:relative;\"><a href=\"#6-in-operator\" aria-label=\"6 in operator 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>6. $in Operator</h4>\n<p>You can use the $in operator to retrieve the documents where a field’s value is equal to any value in the specified array.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"30\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt;db.orders.find( { OrderItems.ItemName: { $in: [&quot;journal&quot;,&quot;paper&quot;] } } )</span></code></pre>\n<p><strong>Example</strong></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"31\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt;db.Orders.find( { OrderItems.ItemName: { $in: [&quot;journal&quot;,&quot;paper&quot;] } } , { Customer: 1, OrderItems: 1 })</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">\t\t&quot;_id&quot; : ObjectId(&quot;5dd4e2cc0821d3b44607534c&quot;)</span>\n<span class=\"grvsc-line\">\t\tCustomer: &quot;abc&quot;,</span>\n<span class=\"grvsc-line\">\t\tOrderItems:[</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;notebook&quot;,&quot;Price&quot;:&quot;150.00&quot;,&quot;Qty&quot;:10},</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;paper&quot;,&quot;Price&quot;:&quot;10.00&quot;,&quot;Qty&quot;:5},</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;journal&quot;,&quot;Price&quot;:&quot;200.00&quot;,&quot;Qty&quot;:2},</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;postcard&quot;,&quot;Price&quot;:&quot;10.00&quot;,&quot;Qty&quot;:500}</span>\n<span class=\"grvsc-line\">\t\t]\t\t</span>\n<span class=\"grvsc-line\">\t},</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">\t\t&quot;_id&quot; : ObjectId(&quot;5dd4e2cc0821d3b44607544c&quot;),</span>\n<span class=\"grvsc-line\">\t\tCustomer: &quot;xyz&quot;,</span>\n<span class=\"grvsc-line\">\t\tOrderItems:[</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;notebook&quot;,&quot;Price&quot;:&quot;150.00&quot;,&quot;Qty&quot;:5},</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;paper&quot;,&quot;Price&quot;:&quot;10.00&quot;,&quot;Qty&quot;:5},</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;postcard&quot;,&quot;Price&quot;:&quot;10.00&quot;,&quot;Qty&quot;:500}</span>\n<span class=\"grvsc-line\">\t\t]</span>\n<span class=\"grvsc-line\">\t}</span>\n<span class=\"grvsc-line\">&gt;</span></code></pre>\n<h4 id=\"7-nin-operator\" style=\"position:relative;\"><a href=\"#7-nin-operator\" aria-label=\"7 nin operator 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>7. $nin Operator</h4>\n<p>You can use the $nin operator to retrieve the documents where a field’s value is not equal to any value in the specified array. It will also select the documents where the field does not exist.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"32\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt;db.orders.find( { OrderItems.ItemName: { $nin: [&quot;journal&quot;,&quot;paper&quot;] } } )</span></code></pre>\n<p><strong>Example</strong></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"33\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt;db.Orders.find( { OrderItems.ItemName: { $nin: [&quot;journal&quot;,&quot;paper&quot;] } } , { Customer: 1, OrderItems: 1 })</span>\n<span class=\"grvsc-line\">\t{</span>\n<span class=\"grvsc-line\">\t\t&quot;_id&quot; : ObjectId(&quot;5dd4e2cc0821d3b44607644c&quot;),</span>\n<span class=\"grvsc-line\">\t\tCustomer: &quot;ron&quot;,</span>\n<span class=\"grvsc-line\">\t\tOrderItems:[</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;notebook&quot;,&quot;Price&quot;:&quot;150.00&quot;,&quot;Qty&quot;:5},</span>\n<span class=\"grvsc-line\">\t\t\t{&quot;ItemName&quot;:&quot;postcard&quot;,&quot;Price&quot;:&quot;10.00&quot;,&quot;Qty&quot;:00}</span>\n<span class=\"grvsc-line\">\t\t]</span>\n<span class=\"grvsc-line\">\t}</span>\n<span class=\"grvsc-line\">&gt;</span></code></pre>\n<h3 id=\"indexing\" style=\"position:relative;\"><a href=\"#indexing\" aria-label=\"indexing 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>Indexing</h3>\n<p>We know that <a href=\"https://www.loginradius.com/blog/engineering/index-in-mongodb/\">indexing is very important</a> if we are performing the queries on a large database. Without indexing execution of a query can be expensive. We can add a simple ascending index on a single field by using the following statement.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"34\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">&gt;db.Orders.createIndex({&quot;Customer&quot;:1})</span></code></pre>\n<p>MongoDB creates a unique index on ‘_id’ field by default. A unique index will prevent insertion of two documents with the same value for that field. If you want to create a unique index then the statement would be as follows.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"35\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">db.Orders.createIndex( { &quot;OrderId&quot;: 1 }, { unique: true } )</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, If you want to learn few more stuff on MongoDB, 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 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>","frontmatter":{"title":"How to Perform Basic Query Operations in MongoDB","author":{"id":"Anil Gupta","github":"anilswm","avatar":null},"date":"January 20, 2021","updated_date":null,"tags":["MongoDB"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.5037593984962405,"src":"/static/f4c86ce656fbbcd1cebd60b0c6606d53/58556/coverImage.webp","srcSet":"/static/f4c86ce656fbbcd1cebd60b0c6606d53/61e93/coverImage.webp 200w,\n/static/f4c86ce656fbbcd1cebd60b0c6606d53/1f5c5/coverImage.webp 400w,\n/static/f4c86ce656fbbcd1cebd60b0c6606d53/58556/coverImage.webp 800w,\n/static/f4c86ce656fbbcd1cebd60b0c6606d53/99238/coverImage.webp 1200w,\n/static/f4c86ce656fbbcd1cebd60b0c6606d53/7c22d/coverImage.webp 1600w,\n/static/f4c86ce656fbbcd1cebd60b0c6606d53/25f09/coverImage.webp 1920w","sizes":"(max-width: 800px) 100vw, 800px"}}}},"fields":{"authorId":"Anil Gupta","slug":"/engineering/basic-query-operations-in-mongodb/"}}}]},"authorYaml":{"id":"Anil Gupta","bio":null,"github":"anilswm","stackoverflow":null,"linkedin":null,"medium":null,"twitter":null,"avatar":null}},"pageContext":{"id":"Anil Gupta","__params":{"id":"anil-gupta"}}},"staticQueryHashes":["1171199041","1384082988","2100481360","23180105","528864852"]}