{"componentChunkName":"component---src-pages-markdown-remark-fields-slug-js","path":"/engineering/basic-query-operations-in-mongodb/","result":{"data":{"markdownRemark":{"id":"29ac238e-b3d9-5f78-83c0-ec97abe366ad","excerpt":"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…","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>","headings":[{"value":"Create a Sample Database","depth":2},{"value":"Query Documents","depth":2},{"value":"find() method","depth":3},{"value":"Projection","depth":3},{"value":"Filter the Documents by Specifying a Condition","depth":3},{"value":"1. $eq Operator","depth":4},{"value":"2. $gt Operator","depth":4},{"value":"3. $gte Operator","depth":4},{"value":"4. $lt Operator","depth":4},{"value":"4. $lte Operator","depth":4},{"value":"5. $ne Operator","depth":4},{"value":"6. $in Operator","depth":4},{"value":"7. $nin Operator","depth":4},{"value":"Indexing","depth":3},{"value":"Conclusion","depth":2}],"fields":{"slug":"/engineering/basic-query-operations-in-mongodb/"},"frontmatter":{"metatitle":null,"metadescription":null,"description":null,"title":"How to Perform Basic Query Operations in MongoDB","canonical":null,"date":"January 20, 2021","updated_date":null,"tags":["MongoDB"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.5037593984962405,"src":"/static/f4c86ce656fbbcd1cebd60b0c6606d53/2ad7f/coverImage.webp","srcSet":"/static/f4c86ce656fbbcd1cebd60b0c6606d53/1c9b5/coverImage.webp 200w,\n/static/f4c86ce656fbbcd1cebd60b0c6606d53/f1752/coverImage.webp 400w,\n/static/f4c86ce656fbbcd1cebd60b0c6606d53/2ad7f/coverImage.webp 800w,\n/static/f4c86ce656fbbcd1cebd60b0c6606d53/e7405/coverImage.webp 1200w,\n/static/f4c86ce656fbbcd1cebd60b0c6606d53/d3cba/coverImage.webp 1600w,\n/static/f4c86ce656fbbcd1cebd60b0c6606d53/e7136/coverImage.webp 1920w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Anil Gupta","github":"anilswm","bio":null,"avatar":null}}}},"pageContext":{"id":"29ac238e-b3d9-5f78-83c0-ec97abe366ad","fields__slug":"/engineering/basic-query-operations-in-mongodb/","__params":{"fields__slug":"engineering"}}},"staticQueryHashes":["1171199041","1384082988","1711371485","1753898100","2100481360","229320306","23180105","528864852"]}