Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>That was funny i cannot decide to say it is a bug or not it is up to you:</p> <p>There are two available syntax: <a href="http://docs.mongodb.org/manual/reference/operator/query/">http://docs.mongodb.org/manual/reference/operator/query/</a></p> <p>When you using: </p> <pre><code>db.collection.find( { age : 25 } ) </code></pre> <p>also will </p> <pre><code>db.collection.find( { age : 25 } ).explain() db.collection.find( { age : 25 } ).hint(someindex) </code></pre> <p>work fine.</p> <p>When you using your solution (the other syntax):</p> <pre><code>db.collection.find( { $query: { age : 25 } } ) </code></pre> <p>the output of </p> <pre><code>db.sampleCollection.find({$query:{"stringField":"Random string0"}}).explain() </code></pre> <p>Will show like the query not using the index</p> <p>if you also use .hint for the index it will omit the result. :) (That is i do not really understand)</p> <p>Fortunately there is another syntax for these operations too: you can use:</p> <pre><code>db.sampleCollection.find({$query:{"stringField":"Random string0"}, $explain:1}) </code></pre> <p>it will have the right output and showed for me the usage of the index. Also there is similar syntax for $hint.</p> <p>You can check the documentation here: <a href="http://docs.mongodb.org/manual/reference/meta-query-operators/">http://docs.mongodb.org/manual/reference/meta-query-operators/</a></p> <p>I found this really interesting so i turned on the profiler: </p> <p>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.</p> <p>For this query:</p> <pre><code>db.queryTst.find({$query:{"age":16},$explain:1}) </code></pre> <p>i got:</p> <pre><code>{ "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" : "" } </code></pre> <p>for this:</p> <pre><code> db.queryTst.find({$query:{"age":16},$explain:1}).explain() </code></pre> <p>i got:</p> <pre><code>"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" : { }, </code></pre> <p>in the profiler log: for the first</p> <pre><code>{ "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" : "" } </code></pre> <p>for the second:</p> <pre><code>{ "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" : "" } </code></pre> <p>which somehow means to me that is the explain() cause the table scan in the mixed syntax.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload