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
Newsletter
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

  • Part 2: Securing and Scaling Goose-to-Java Agent Traffic With agentgateway
  • Why Your AI Agent Works in the Demo and Fails in Production
  • Claude Code Superpowers: The 5-Tool Stack That Ends Limits and Context Amnesia
  • The Window Closes: Why I Switched to Kotlin Before the Codebase Got Too Big

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
May. 01, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
9.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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