0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00
mongodb/jstests/not2.js

140 lines
3.5 KiB
JavaScript
Raw Normal View History

2010-02-22 23:33:24 +01:00
t = db.jstests_not2;
t.drop();
check = function( query, expected, size ) {
if ( size == null ) {
size = 1;
}
assert.eq( size, t.count( query ), tojson( query ) );
if ( size > 0 ) {
assert.eq( expected, t.findOne( query ).i, tojson( query ) );
}
}
2010-02-23 01:24:17 +01:00
fail = function( query ) {
2010-02-23 23:20:28 +01:00
try {
t.count( query );
} catch ( e ) {
}
2010-02-23 01:24:17 +01:00
assert( db.getLastError(), tojson( query ) );
}
doTest = function() {
t.remove( {} );
2010-02-22 23:33:24 +01:00
t.save( {i:"a"} );
t.save( {i:"b"} );
2010-02-23 01:24:17 +01:00
fail( {i:{$not:"a"}} );
2010-02-23 19:29:10 +01:00
fail( {i:{$not:{$not:{$gt:"a"}}}} );
fail( {i:{$not:{$ref:"foo"}}} );
fail( {i:{$not:{}}} );
check( {i:{$gt:"a"}}, "b" );
2010-02-22 23:33:24 +01:00
check( {i:{$not:{$gt:"a"}}}, "a" );
2010-02-23 19:29:10 +01:00
check( {i:{$not:{$ne:"a"}}}, "a" );
2010-02-22 23:33:24 +01:00
check( {i:{$not:{$gte:"b"}}}, "a" );
check( {i:{$exists:true}}, "a", 2 );
check( {i:{$not:{$exists:true}}}, "", 0 );
check( {j:{$not:{$exists:false}}}, "", 0 );
check( {j:{$not:{$exists:true}}}, "a", 2 );
check( {i:{$not:{$in:["a"]}}}, "b" );
check( {i:{$not:{$in:["a", "b"]}}}, "", 0 );
check( {i:{$not:{$in:["g"]}}}, "a", 2 );
check( {i:{$not:{$nin:["a"]}}}, "a" );
check( {i:{$not:/a/}}, "b" );
2010-02-23 01:24:17 +01:00
check( {i:{$not:/(a|b)/}}, "", 0 );
check( {i:{$not:/a/,$regex:"a"}}, "", 0 );
2010-02-22 23:33:24 +01:00
check( {i:{$not:/aa/}}, "a", 2 );
2010-02-23 01:24:17 +01:00
fail( {i:{$not:{$regex:"a"}}} );
fail( {i:{$not:{$options:"a"}}} );
2010-02-23 23:20:28 +01:00
check( {i:{$type:2}}, "a", 2 );
check( {i:{$not:{$type:1}}}, "a", 2 );
2010-02-23 19:29:10 +01:00
check( {i:{$not:{$type:2}}}, "", 0 );
check( {i:{$not:{$gt:"c",$lt:"b"}}}, "b" );
2010-02-23 01:24:17 +01:00
t.remove( {} );
2010-02-23 01:24:17 +01:00
t.save( {i:1} );
check( {i:{$not:{$mod:[5,1]}}}, null, 0 );
check( {i:{$mod:[5,2]}}, null, 0 );
check( {i:{$not:{$mod:[5,2]}}}, 1, 1 );
2010-02-22 23:33:24 +01:00
t.remove( {} );
2010-02-23 19:29:10 +01:00
t.save( {i:["a","b"]} );
check( {i:{$not:{$size:2}}}, null, 0 );
check( {i:{$not:{$size:3}}}, ["a","b"] );
check( {i:{$not:{$gt:"a"}}}, null, 0 );
check( {i:{$not:{$gt:"c"}}}, ["a","b"] );
check( {i:{$not:{$all:["a","b"]}}}, null, 0 );
check( {i:{$not:{$all:["c"]}}}, ["a","b"] );
2010-02-23 23:20:28 +01:00
t.remove( {} );
2010-02-23 23:20:28 +01:00
t.save( {i:{j:"a"}} );
t.save( {i:{j:"b"}} );
check( {i:{$not:{$elemMatch:{j:"a"}}}}, {j:"b"} );
check( {i:{$not:{$elemMatch:{j:"f"}}}}, {j:"a"}, 2 );
2010-02-23 19:29:10 +01:00
}
doTest();
t.ensureIndex( {i:1} );
doTest();
2010-02-23 19:29:10 +01:00
t.drop();
t.save( {i:"a"} );
t.save( {i:"b"} );
t.ensureIndex( {i:1} );
2010-02-22 23:33:24 +01:00
2010-02-23 21:49:22 +01:00
indexed = function( query, min, max ) {
2010-02-23 19:29:10 +01:00
exp = t.find( query ).explain();
2010-02-23 23:41:05 +01:00
// printjson( exp );
2010-02-23 19:29:10 +01:00
assert( exp.cursor.match( /Btree/ ), tojson( query ) );
assert( exp.allPlans.length == 1, tojson( query ) );
2010-02-23 23:41:05 +01:00
// just expecting one element per key
for( i in exp.startKey ) {
assert.eq( exp.startKey[ i ], min );
}
for( i in exp.endKey ) {
assert.eq( exp.endKey[ i ], max );
}
2010-02-23 19:29:10 +01:00
}
2010-02-22 23:33:24 +01:00
2010-02-23 19:29:10 +01:00
not = function( query ) {
exp = t.find( query ).explain();
2010-02-23 23:41:05 +01:00
// printjson( exp );
2010-02-23 19:29:10 +01:00
assert( !exp.cursor.match( /Btree/ ), tojson( query ) );
assert( exp.allPlans.length == 1, tojson( query ) );
}
2010-02-23 21:49:22 +01:00
indexed( {i:1}, 1, 1 );
not( {i:{$ne:1}} );
2010-02-23 23:20:28 +01:00
indexed( {i:{$not:{$ne:"a"}}}, "a", "a" );
not( {i:{$not:/^a/}} );
2010-02-23 23:41:05 +01:00
indexed( {i:{$gt:"a"}}, "a", {} );
indexed( {i:{$not:{$gt:"a"}}}, "", "a" );
indexed( {i:{$gte:"a"}}, "a", {} );
indexed( {i:{$not:{$gte:"a"}}}, "", "a" );
indexed( {i:{$lt:"b"}}, "", "b" );
indexed( {i:{$not:{$lt:"b"}}}, "b", {} );
indexed( {i:{$lte:"b"}}, "", "b" );
indexed( {i:{$not:{$lte:"b"}}}, "b", {} );
not( {i:{$not:{$all:["a"]}}} );
not( {i:{$not:{$mod:[2,1]}}} );
not( {i:{$not:{$type:2}}} );
indexed( {i:{$in:[1]}}, 1, 1 );
not( {i:{$not:{$in:[1]}}} );
t.drop();
t.ensureIndex( {"i.j":1} );
indexed( {i:{$elemMatch:{j:1}}}, 1, 1 );
not( {i:{$not:{$elemMatch:{j:1}}}} );
indexed( {i:{$not:{$elemMatch:{j:{$ne:1}}}}}, 1, 1 );