diff options
author | Randolph Tan <randolph@10gen.com> | 2014-01-14 14:09:42 -0500 |
---|---|---|
committer | Randolph Tan <randolph@10gen.com> | 2014-02-28 16:26:33 -0500 |
commit | 5595b945603b0712c537787e31e6da661c424fee (patch) | |
tree | 90945ee3fe4931032f3af2d397bb755fbf5d30ef /jstests/core/exists6.js | |
parent | cd62080dcb036e83f8fca6d68d9bcab67bf7a21c (diff) | |
download | mongo-5595b945603b0712c537787e31e6da661c424fee.tar.gz |
SERVER-12127 migrate js tests to jscore suite when not related to writes
Moved test jstest/[a-i].js -> jstests/core/ and made changes to comply with write command api
Diffstat (limited to 'jstests/core/exists6.js')
-rw-r--r-- | jstests/core/exists6.js | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/jstests/core/exists6.js b/jstests/core/exists6.js new file mode 100644 index 00000000000..2fa4ba85d49 --- /dev/null +++ b/jstests/core/exists6.js @@ -0,0 +1,79 @@ +// SERVER-393 Test indexed matching with $exists. + +t = db.jstests_exists6; +t.drop(); + +t.ensureIndex( {b:1} ); +t.save( {} ); +t.save( {b:1} ); +t.save( {b:null} ); + +//--------------------------------- + +function checkIndexUse( query, usesIndex, index, bounds ) { + var x = t.find( query ).explain() + if ( usesIndex ) { + assert.eq( x.cursor.indexOf(index), 0 , tojson(x) ); + if ( ! x.indexBounds ) x.indexBounds = {} + assert.eq( bounds, x.indexBounds.b , tojson(x) ); + } + else { + assert.eq( 'BasicCursor', x.cursor, tojson(x) ); + } +} + +function checkExists( query, usesIndex, bounds ) { + checkIndexUse( query, usesIndex, 'BtreeCursor b_1', bounds ); + // Whether we use an index or not, we will always scan all docs. + assert.eq( 3, t.find( query ).explain().nscanned ); + // 2 docs will match. + assert.eq( 2, t.find( query ).itcount() ); +} + +function checkMissing( query, usesIndex, bounds ) { + checkIndexUse( query, usesIndex, 'BtreeCursor b_1', bounds ); + // Nscanned changes based on index usage. + if ( usesIndex ) assert.eq( 2, t.find( query ).explain().nscanned ); + else assert.eq( 3, t.find( query ).explain().nscanned ); + // 1 doc is missing 'b'. + assert.eq( 1, t.find( query ).itcount() ); +} + +function checkExistsCompound( query, usesIndex, bounds ) { + checkIndexUse( query, usesIndex, 'BtreeCursor', bounds ); + if ( usesIndex ) assert.eq( 3, t.find( query ).explain().nscanned ); + else assert.eq( 3, t.find( query ).explain().nscanned ); + // 2 docs have a:1 and b:exists. + assert.eq( 2, t.find( query ).itcount() ); +} + +function checkMissingCompound( query, usesIndex, bounds ) { + checkIndexUse( query, usesIndex, 'BtreeCursor', bounds ); + // two possible indexes to use + // 1 doc should match + assert.eq( 1, t.find( query ).itcount() ); +} + +//--------------------------------- + +var allValues = [ [ { $minElement:1 }, { $maxElement:1 } ] ]; +var nullNull = [ [ null, null ] ]; + +// Basic cases +checkExists( {b:{$exists:true}}, true, allValues ); +// We change this to not -> not -> exists:true, and get allValue for bounds +// but we use a BasicCursor? +checkExists( {b:{$not:{$exists:false}}}, false, allValues ); +checkMissing( {b:{$exists:false}}, true, nullNull ); +checkMissing( {b:{$not:{$exists:true}}}, true, nullNull ); + +// Now check existence of second compound field. +t.ensureIndex( {a:1,b:1} ); +t.save( {a:1} ); +t.save( {a:1,b:1} ); +t.save( {a:1,b:null} ); + +checkExistsCompound( {a:1,b:{$exists:true}}, true, allValues ); +checkExistsCompound( {a:1,b:{$not:{$exists:false}}}, true, allValues ); +checkMissingCompound( {a:1,b:{$exists:false}}, true, nullNull ); +checkMissingCompound( {a:1,b:{$not:{$exists:true}}}, true, nullNull ); |