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

  • Home
  • SEARCH
  • 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 9213571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:46:46+00:00 2026-06-18T01:46:46+00:00

I created a test MongoDB collection sampleCollection with documents which looks like: _id :

  • 0

I created a test MongoDB collection “sampleCollection” with documents which looks like:

 "_id" : ObjectId("510929e041cb2179b41ace1c"),
 "stringField" : "Random string0",
 "longField" : NumberLong(886)

and has index on field “stringField”.
When I execute

db.sampleCollection.find({"stringField":"Random string0"}).explain()

everything is ok:

 "cursor" : "BtreeCursor stringField_1",
 "isMultiKey" : false,
 "n" : 2,
 "nscannedObjects" : 2,
 "nscanned" : 2,
 "nscannedObjectsAllPlans" : 2,
 "nscannedAllPlans" : 2,
 "scanAndOrder" : false,
 "indexOnly" : false,
 "nYields" : 0,
 "nChunkSkips" : 0,
 "millis" : 0,
 "indexBounds" : {
         "stringField" : [
                 [
                         "Random string0",
                         "Random string0"
                 ]
         ]
 }

but

db.sampleCollection.find({$query:{"stringField":"Random string0"}}).explain()

gets me

"cursor" : "BasicCursor",
"isMultiKey" : false,
"n" : 0,
"nscannedObjects" : 4,
"nscanned" : 4,
"nscannedObjectsAllPlans" : 4,
"nscannedAllPlans" : 4,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {

}

This is not looks like a problem, but I’m using org.springframework.data.mongodb framework in production and usage of query constructions is an only way to write repositories.
And thus I have a db which completely ignores indexed data.

Is it correct? Or I misunderstood something?

  • 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-18T01:46:47+00:00Added an answer on June 18, 2026 at 1:46 am

    That was funny i cannot decide to say it is a bug or not it is up to you:

    There are two available syntax: http://docs.mongodb.org/manual/reference/operator/query/

    When you using:

    db.collection.find( { age : 25 } )
    

    also will

    db.collection.find( { age : 25 } ).explain()
    db.collection.find( { age : 25 } ).hint(someindex)
    

    work fine.

    When you using your solution (the other syntax):

    db.collection.find( { $query: { age : 25 } } )
    

    the output of

    db.sampleCollection.find({$query:{"stringField":"Random string0"}}).explain()
    

    Will show like the query not using the index

    if you also use .hint for the index it will omit the result. 🙂 (That is i do not really understand)

    Fortunately there is another syntax for these operations too: you can use:

    db.sampleCollection.find({$query:{"stringField":"Random string0"}, $explain:1})
    

    it will have the right output and showed for me the usage of the index. Also there is similar syntax for $hint.

    You can check the documentation here: http://docs.mongodb.org/manual/reference/meta-query-operators/

    I found this really interesting so i turned on the profiler:

    i made a test collection (queryTst) with around 250k docs each with only _id and an age field in the structure with an index on age.

    For this query:

    db.queryTst.find({$query:{"age":16},$explain:1})
    

    i got:

    {
        "cursor" : "BtreeCursor age_1",
        "isMultiKey" : false,
        "n" : 2,
        "nscannedObjects" : 2,
        "nscanned" : 2,
        "nscannedObjectsAllPlans" : 2,
        "nscannedAllPlans" : 2,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nYields" : 0,
        "nChunkSkips" : 0,
        "millis" : 0,
        "indexBounds" : {
            "age" : [
                [
                    16,
                    16
                ]
            ]
        },
        "allPlans" : [
            {
                "cursor" : "BtreeCursor age_1",
                "n" : 2,
                "nscannedObjects" : 2,
                "nscanned" : 2,
                "indexBounds" : {
                    "age" : [
                        [
                            16,
                            16
                        ]
                    ]
                }
            }
        ],
        "oldPlan" : {
            "cursor" : "BtreeCursor age_1",
            "indexBounds" : {
                "age" : [
                    [
                        16,
                        16
                    ]
                ]
            }
        },
        "server" : ""
    }
    

    for this:

     db.queryTst.find({$query:{"age":16},$explain:1}).explain()
    

    i got:

    "cursor" : "BasicCursor",
        "isMultiKey" : false,
        "n" : 0,
        "nscannedObjects" : 250011,
        "nscanned" : 250011,
        "nscannedObjectsAllPlans" : 250011,
        "nscannedAllPlans" : 250011,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nYields" : 0,
        "nChunkSkips" : 0,
        "millis" : 103,
        "indexBounds" : {
    
        },
    

    in the profiler log: for the first

    {
        "ts" : ISODate("2013-01-30T20:35:40.526Z"),
        "op" : "query",
        "ns" : "test.queryTst",
        "query" : {
            "$query" : {
                "age" : 16
            },
            "$explain" : 1
        },
        "ntoreturn" : 0,
        "ntoskip" : 0,
        "nscanned" : 2,
        "keyUpdates" : 0,
        "numYield" : 0,
        "lockStats" : {
            "timeLockedMicros" : {
                "r" : NumberLong(368),
                "w" : NumberLong(0)
            },
            "timeAcquiringMicros" : {
                "r" : NumberLong(8),
                "w" : NumberLong(5)
            }
        },
        "nreturned" : 1,
        "responseLength" : 567,
        "millis" : 0,
        "client" : "127.0.0.1",
        "user" : ""
    }
    

    for the second:

    {
        "ts" : ISODate("2013-01-30T20:35:47.715Z"),
        "op" : "query",
        "ns" : "test.queryTst",
        "query" : {
            "query" : {
                "$query" : {
                    "age" : 16
                },
                "$explain" : 1
            },
            "$explain" : true
        },
        "ntoreturn" : 0,
        "ntoskip" : 0,
        "nscanned" : 250011,
        "keyUpdates" : 0,
        "numYield" : 0,
        "lockStats" : {
            "timeLockedMicros" : {
                "r" : NumberLong(104092),
                "w" : NumberLong(0)
            },
            "timeAcquiringMicros" : {
                "r" : NumberLong(13),
                "w" : NumberLong(5)
            }
        },
        "nreturned" : 1,
        "responseLength" : 373,
        "millis" : 104,
        "client" : "127.0.0.1",
        "user" : ""
    }
    

    which somehow means to me that is the explain() cause the table scan in the mixed syntax.

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

Sidebar

Related Questions

I created a test application that generates 10k random numbers in a range from
I have created a test table in MySQL and would like to insert 10
I created a small Java application (with Spring Data) to test MongoDB performance with
I have a collection full of documents with a created_date attribute. I'd like to
I have a specs2 test which uses a FakeApplication and an embedded mongodb database.
I created test.l, input to flex, which ends with the main function. When the
I have created 5 test cases in JUnit 4 and I was wondering is
I'm currently studying the Mockito framework and I've created several test cases using Mockito.
I have my new macbook air. I have created one test application. now i
I created a empty test project (static library type) with enabling the unit test.

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.