

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Multi-key Indexing on an entire array with MongoDB?
<p>Let us first create a collection with documents −</p><pre class="prettyprint notranslate">> db.demo277.insertOne({"details":[{"FirstName":"John"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e48fb21dd099650a5401a52") } > db.demo277.insertOne({"details":[{"FirstName":"David"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e48fb27dd099650a5401a53") } > db.demo277.insertOne({"details":[{"FirstName":"Chris"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e48fb2bdd099650a5401a54") }</pre><p>Display all documents from a collection with the help of find() method −</p><pre class="prettyprint notranslate">> db.demo277.find();</pre><p>This will produce the following output −</p><pre class="result notranslate">{ "_id" : ObjectId("5e48fb21dd099650a5401a52"), "details" : [ { "FirstName" : "John" } ] } { "_id" : ObjectId("5e48fb27dd099650a5401a53"), "details" : [ { "FirstName" : "David" } ] } { "_id" : ObjectId("5e48fb2bdd099650a5401a54"), "details" : [ { "FirstName" : "Chris" } ] }</pre><p>Following is the query to implement multi-key indexing on an entire array. It does scan full documents −</p><pre class="prettyprint notranslate">> db.demo277.find({"details":{FirstName:"David"}});</pre><p>This will produce the following output −</p><pre class="result notranslate">{ "_id" : ObjectId("5e48fb27dd099650a5401a53"), "details" : [ { "FirstName" : "David" } ] }</pre>
- Related Questions & Answers
- Working with MongoDB $concatArrays in $project on existing multi-array field
- Query on the last object of an array with MongoDB
- MongoDB query to remove entire array from collection?
- Array index or indexing inner items in MongoDB to fetch values
- Implement $dateToString on array items with MongoDB
- MongoDB multiple OR conditions on same key?
- Update object in array with a specific key in MongoDB
- Querying on an array of objects for specific nested documents with MongoDB?
- Changing the primary key on a MongoDB collection?
- How to delete an array element based on key in PHP?
- Replace an array field value with MongoDB?
- Updating an array with $push in MongoDB
- How to clear an entire Treeview with Tkinter?
- Filter query on array of embedded document with MongoDB?
- How do you perform an AND query on an array in MongoDB?
Advertisements