Covered query in MongoDB

You are here : Home / MongoDB Tutorial


Contents of page >


1) What are covered query in MongoDB?


Covered query is a query in which >
  • All the fields in the query are part of index. And
  • All the fields returned in the query are in the same index.
2) What is advantage of using covered query in MongoDB?
All the data in this case is fetched very quickly (from same index - which was already loaded into RAM) in extremely efficient manner without scanning such large data(documents). We will discus example further in this article.


3) Let's create new collection and insert document in it >
> db.employee.insert({firstName:"Ankit", lastName:"Mittal", salary : 1000 })
> db.employee.insert({firstName:"Neha", lastName:"Kati", salary : 2000 })
Above will create collection (or table) (if collection already exists it will insert documents in it).


4) Creating Covered Index (create compound index on field firstName and salary) >
> db.employee.ensureIndex({firstName  : 1}, {salary : 1} )
Now, covered index have been created on field firstName and salary.

5) Now, let's query/read data from covered indexes >


5.1) Now, covered index will be available when we use following query >
db.employee.find({firstName:"Ankit"},{salary:1, _id :0})
Output>
{ "salary" : 1000 }
In above query,
when we find document where firstName="Ankit", covered index (on firstName and salary) is loaded into RAM, and as we know that salary is also present in compound index it is read very quickly, and we avoid scanning of too many documents in database.

5.2) Now, covered index will NOT be available when we use following query >
db.employee.find({firstName:"Ankit"},{salary:1})
Output>
{ "_id" : ObjectId("588103a41e509423fa0faa6b"), "salary" : 1000 }
In above query,
when we find document where firstName="Ankit", covered index (on firstName and salary) is loaded into RAM, but by default _id (i.e the field not present in covered index) is also displayed so despite of salary present in compound index, we CANNOT avoid scanning of too many documents in database.


5.3) Now, covered index will also NOT be available when we use following query >
db.employee.find({firstName:"Ankit"})
Output>
{ "_id" : ObjectId("588103a41e509423fa0faa6b"), "firstName" : "Ankit", "lastName" : "Mittal", "salary" : 1000 }
Here we can see that fields (_id and lastName) not present in covered index are displayed.

6) Where covered index does not work?
covered index does not work when field on which we are trying to cover in covered index >
  • Array or
  • Embedded document (or a sub document)

7) Summary -
So in this MongoDB tutorial we learned about covered query in MongoDB.


Covered query is a query in which >
  • All the fields in the query are part of index. And
  • All the fields returned in the query are in the same index.
All the data fetched using covered query is fetched very quickly.
Creating Covered Index (create compound index on field firstName and salary)>
> db.employee.ensureIndex({firstName  : 1}, {salary : 1} )
Now, index have been created on field empId.


Now, let's query/read data from covered indexes >
db.employee.find({firstName:"Ankit"},{salary:1, _id :0})
In above query,  when we find document where firstName="Ankit", covered index (on firstName and salary) is loaded into RAM, and as we know that salary is also present in compound index it is read very quickly.



Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter. You may join our fbGroup or linkedInGroup as well.


RELATED LINKS>

What is MongoDB - A quick introduction to database


Labels: MongoDB
eEdit
Must read for you :