diff options
author | Hari Khalsa <hkhalsa@10gen.com> | 2014-01-17 14:54:43 -0500 |
---|---|---|
committer | Hari Khalsa <hkhalsa@10gen.com> | 2014-01-17 15:15:00 -0500 |
commit | 3b5b31f42d390e9da23f66e78a300d997ae653c4 (patch) | |
tree | 15f4a0cc2fe004676e04ab01e84123d9484dc6ac | |
parent | 1b44a81f70869fd24e2eccf1cfa2faa9bc372e45 (diff) | |
download | mongo-3b5b31f42d390e9da23f66e78a300d997ae653c4.tar.gz |
SERVER-2094 distinct only wants the field it's distinct-ing over
-rw-r--r-- | jstests/distinct_index1.js | 12 | ||||
-rw-r--r-- | src/mongo/db/commands/distinct.cpp | 16 |
2 files changed, 16 insertions, 12 deletions
diff --git a/jstests/distinct_index1.js b/jstests/distinct_index1.js index 9e2089858b9..e6687d7b762 100644 --- a/jstests/distinct_index1.js +++ b/jstests/distinct_index1.js @@ -37,14 +37,11 @@ t.ensureIndex( { a : 1 } ) x = d( "a" ); assert.eq( 1000 , x.stats.n , "BA1" ) assert.eq( 1000 , x.stats.nscanned , "BA2" ) -// QUERY_MIGRATION: pending projection support -//assert.eq( 0 , x.stats.nscannedObjects , "BA3" ) x = d( "a" , { a : { $gt : 5 } } ); assert.eq( 398 , x.stats.n , "BB1" ) assert.eq( 398 , x.stats.nscanned , "BB2" ) -// QUERY_MIGRATION: pending projection support -// assert.eq( 0 , x.stats.nscannedObjects , "BB3" ) +assert.eq( 0 , x.stats.nscannedObjects , "BB3" ) x = d( "b" , { a : { $gt : 5 } } ); assert.eq( 398 , x.stats.n , "BC1" ) @@ -55,10 +52,5 @@ assert.eq( 398 , x.stats.nscannedObjects , "BC3" ) t.dropIndexes(); t.ensureIndex( { a : 1, b : 1 } ); x = d( "b" , { a : { $gt : 5 }, b : { $gt : 5 } } ); -// QUERY_MIGRATION: we show the actual cursor used -// assert.eq( "QueryOptimizerCursor", x.stats.cursor ); assert.eq( 171 , x.stats.n ) -// QUERY_MIGRATION: our nscanned is lower... -// assert.eq( 275 , x.stats.nscanned ) -// Disable temporarily - exact value doesn't matter. -// assert.eq( 266 , x.stats.nscannedObjects ) +assert.eq( 0 , x.stats.nscannedObjects , "BB3" ) diff --git a/src/mongo/db/commands/distinct.cpp b/src/mongo/db/commands/distinct.cpp index f22a407a941..503beedf4dc 100644 --- a/src/mongo/db/commands/distinct.cpp +++ b/src/mongo/db/commands/distinct.cpp @@ -100,8 +100,15 @@ namespace mongo { } CanonicalQuery* cq; - // XXX: project out just the field we're distinct-ing. May be covered... - if (!CanonicalQuery::canonicalize(ns, query, &cq).isOK()) { + + // We only care about the field that we're projecting over. Have to drop the _id field + // explicitly because those are .find() semantics. + // + // Applying a projection allows the planner to try to give us covered plans. + BSONObj projection = BSON("_id" << 0 << key << 1); + + // Apply a projection of the key. Empty BSONObj() is for the sort. + if (!CanonicalQuery::canonicalize(ns, query, BSONObj(), projection, &cq).isOK()) { uasserted(17215, "Can't canonicalize query " + query.toString()); return 0; } @@ -120,6 +127,11 @@ namespace mongo { BSONObj obj; Runner::RunnerState state; while (Runner::RUNNER_ADVANCED == (state = runner->getNext(&obj, NULL))) { + // Distinct expands arrays. + // + // If our query is covered, each value of the key should be in the index key and + // available to us without this. If a collection scan is providing the data, we may + // have to expand an array. BSONElementSet elts; obj.getFieldsDotted(key, elts); |