DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building a 3D WebXR Game with WASI Cycles: Integrating WasmEdge, Wasmtime, and Wasmer to Invoke MongoDB, Kafka, and Oracle
  • A Developer's Guide to Database Sharding With MongoDB
  • Kafka Link: Ingesting Data From MongoDB to Capella Columnar
  • Java and MongoDB Integration: A CRUD Tutorial [Video Tutorial]

Trending

  • Architecting Sub-Microsecond HFT Systems With C++ and Zero-Copy IPC
  • DuckDB for Python Developers
  • Building a Skill-Based Agentic Reviewer with Claude Code: A Practical Guide Using Skills.MD, MCP Servers, Tools, and Tasks
  • Why AI Forces a Rethink of Everything We Know About Software Security
  1. DZone
  2. Data Engineering
  3. Databases
  4. Covered Queries in MongoDB

Covered Queries in MongoDB

Walk through using covered queries in MongoDB in five steps so that all the fields in the query, as well as results returned, are part of the index.

By 
Vijaykishan Shyamsundar user avatar
Vijaykishan Shyamsundar
·
May. 01, 18 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
8.9K Views

Join the DZone community and get the full member experience.

Join For Free

Covered queries help us in querying data faster. This is achieved by ensuring the index created contains all the fields required by the query. It doesn’t require examining any documents apart from the indexed ones.

We need to ensure that all the fields in the query, as well as results returned, are part of the index.

Please note that:

  • Covered queries will not work on arrays and sub-documents.

  • Cannot include _ID on results, e.g. _ID: 0, to be part of queried indexes.

Let’s walk through it with a simple example.

Step 1: Create a collection as follows:

db.products.insert( { "Name":"T-Shirt", "UnitPrice":345.00, "Qty":20 })

Step 2: Create a multi-key index:

db.products.createIndex({"Name":1,"Qty":1})

Step 3: Write a select query with explain(true): 

db.products.find({"Name":"T-Shirt","Qty":20}).explain(true)

Step 4: Watch the result of the previous step.

We inserted only one record and it states that the total docs examined is 1:

"executionStats" : {
"executionSuccess" : true,
  "nReturned" : 1
        "executionTimeMillis" : 0,
  "totalKeysExamined" : 1
        "totalDocsExamind" : 1,

Step 5: The covered query:

db.products.find({"Name":"T-Shirt","Qty":20},{"Name":"T-Shirt","Qty":20,_id:0}).explain(true)

Let me explain why I call the above a covered query.

You may notice that the only difference between Step 4 and Step 5 is _ID: 0.

Yes. That's precisely the point!

In the query, you can see that all the fields are part of the index created and we ensured _id was removed as part of results. That is because _id is the default index and its presence will blow the covered query rule.

The below results clearly indicate that it has not searched any document(s) at all. It just went through the index and found the result.

"executionStats" : {
"executionSuccess" : true,
  "nReturned" : 1
        "executionTimeMillis" : 0,
  "totalKeysExamined" : 1
        "totalDocsExamind" : 0,

I hope this article was useful!



If you enjoyed this article and want to learn more about MongoDB, check out this collection of tutorials and articles on all things MongoDB.

Database MongoDB

Published at DZone with permission of Vijaykishan Shyamsundar. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building a 3D WebXR Game with WASI Cycles: Integrating WasmEdge, Wasmtime, and Wasmer to Invoke MongoDB, Kafka, and Oracle
  • A Developer's Guide to Database Sharding With MongoDB
  • Kafka Link: Ingesting Data From MongoDB to Capella Columnar
  • Java and MongoDB Integration: A CRUD Tutorial [Video Tutorial]

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook