Multi-key Indexing on an entire array with MongoDB?

MongoDBDatabaseBig Data Analytics

<p>Let us first create a collection with documents &minus;</p><pre class="prettyprint notranslate">&gt; db.demo277.insertOne({&quot;details&quot;:[{&quot;FirstName&quot;:&quot;John&quot;}]}); { &nbsp; &nbsp;&quot;acknowledged&quot; : true, &nbsp; &nbsp;&quot;insertedId&quot; : ObjectId(&quot;5e48fb21dd099650a5401a52&quot;) } &gt; db.demo277.insertOne({&quot;details&quot;:[{&quot;FirstName&quot;:&quot;David&quot;}]}); { &nbsp; &nbsp;&quot;acknowledged&quot; : true, &nbsp; &nbsp;&quot;insertedId&quot; : ObjectId(&quot;5e48fb27dd099650a5401a53&quot;) } &gt; db.demo277.insertOne({&quot;details&quot;:[{&quot;FirstName&quot;:&quot;Chris&quot;}]}); { &nbsp; &nbsp;&quot;acknowledged&quot; : true, &nbsp; &nbsp;&quot;insertedId&quot; : ObjectId(&quot;5e48fb2bdd099650a5401a54&quot;) }</pre><p>Display all documents from a collection with the help of find() method &minus;</p><pre class="prettyprint notranslate">&gt; db.demo277.find();</pre><p>This will produce the following output &minus;</p><pre class="result notranslate">{ &quot;_id&quot; : ObjectId(&quot;5e48fb21dd099650a5401a52&quot;), &quot;details&quot; : [ { &quot;FirstName&quot; : &quot;John&quot; } ] } { &quot;_id&quot; : ObjectId(&quot;5e48fb27dd099650a5401a53&quot;), &quot;details&quot; : [ { &quot;FirstName&quot; : &quot;David&quot; } ] } { &quot;_id&quot; : ObjectId(&quot;5e48fb2bdd099650a5401a54&quot;), &quot;details&quot; : [ { &quot;FirstName&quot; : &quot;Chris&quot; } ] }</pre><p>Following is the query to implement multi-key indexing on an entire array. It does scan full documents &minus;</p><pre class="prettyprint notranslate">&gt; db.demo277.find({&quot;details&quot;:{FirstName:&quot;David&quot;}});</pre><p>This will produce the following output &minus;</p><pre class="result notranslate">{ &quot;_id&quot; : ObjectId(&quot;5e48fb27dd099650a5401a53&quot;), &quot;details&quot; : [ { &quot;FirstName&quot; : &quot;David&quot; } ] }</pre>
raja
Updated on 31-Mar-2020 13:16:35

Advertisements