Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9119619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:26:23+00:00 2026-06-17T05:26:23+00:00

What happens when you query two fields, given that they’re not part of a

  • 0

What happens when you query two fields, given that they’re not part of a compound index, for example:

db.collection.ensureIndex( { a: 1 } )
db.collection.ensureIndex( { b: 1 } )

db.collection.find( { a: 2, b: 2 } )

I’d like to know if a number of documents might be scanned, or if Mongo DB won’t scan anything and the explain() will return:

indexOnly: YES
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:26:24+00:00Added an answer on June 17, 2026 at 5:26 am

    The explain only returns indexOnly if all the values you are looking for are in index (including the sort). Basically it denotes a covered index: http://docs.mongodb.org/manual/applications/indexes/#create-indexes-that-support-covered-queries

    In this case you are querying by two fields but only one of them is in one individual index.

    Considering MongoDB cannot use multiple indexes for a single clause ($or being the exception since it is multi-clause here also it is different than a normal query) it will not do an indexOnly query, instead it will search the collection by the index on a and then do a full document scan of the documents that contain b within that range to understand its values and return a result.

    Do also note that to use indexOnly cursors properly you should make the following parts of the your query fit into a single index:

    • Condition
    • Projection
    • Sort

    i.e.:

    db.ensureIndex({a:1,b:1});
    db.col.find({a:1}, {_id:0,a:1,b:1}).sort({b:1});
    

    Would use a indexOnly cursor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following query where aggregation happens across two tables: Sales and Promo and
When using this SQL query: Select * from table_name what happens if the name
How to change my query so that this error doesn't happen: XML data type
I'm trying to use Google Analytics API to query internal searches that happen on
What happens in the following example? struct B { }; struct D1 : B
What happens to http requests that are being processed when you stop or restart
In my application, I have two queries that occur from time to time (from
I do not want Auditing or History tracking. I have an application that pulls
I have a query that returns a single row of data. Let's say it
I have a Pl/SQL query that pull all records from the 3 tables. That

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.