1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
// Check $match pipeline stage.
// - Filtering behavior equivalent to a mongo query.
// - $where and geo operators are not allowed
load('jstests/aggregation/extras/utils.js');
t = db.jstests_aggregation_match;
t.drop();
identityProjection = { _id:'$_id', a:'$a' };
/** Assert that an aggregation generated the expected error. */
function assertError( expectedCode, matchSpec ) {
matchStage = { $match:matchSpec };
// Check where matching is folded in to DocumentSourceCursor.
assertErrorCode(t, [matchStage], expectedCode)
// Check where matching is not folded in to DocumentSourceCursor.
assertErrorCode(t, [{$project: identityProjection}, matchStage], expectedCode)
}
/** Assert that the contents of two arrays are equal, ignoring element ordering. */
function assertEqualResultsUnordered( one, two ) {
oneStr = one.map( function( x ) { return tojson( x ); } );
twoStr = two.map( function( x ) { return tojson( x ); } );
oneStr.sort();
twoStr.sort();
assert.eq( oneStr, twoStr );
}
/** Assert that an aggregation result is as expected. */
function assertResults( expectedResults, matchSpec ) {
findResults = t.find( matchSpec ).toArray();
if ( expectedResults ) {
assertEqualResultsUnordered( expectedResults, findResults );
}
matchStage = { $match:matchSpec };
// Check where matching is folded in to DocumentSourceCursor.
assertEqualResultsUnordered( findResults, t.aggregate( matchStage ).result );
// Check where matching is not folded in to DocumentSourceCursor.
assertEqualResultsUnordered( findResults,
t.aggregate( { $project:identityProjection },
matchStage ).result );
}
// Invalid matcher syntax.
assertError( 10073, { a:{ $mod:[ 0 /* invalid */, 0 ] } } );
// $where not allowed.
assertError( 16395, { $where:'true' } );
// Geo not allowed.
assertError( 16424, { $match:{ a:{ $near:[ 0, 0 ] } } } );
// Update modifier not allowed.
if ( 0 ) { // SERVER-6650
assertError( 0, { a:1, $inc:{ b:1 } } );
}
// Aggregation expression not allowed.
if ( 0 ) { // SERVER-6650
assertError( 0, { a:1, b:{ $gt:{ $add:[ 1, 1 ] } } } );
}
function checkMatchResults( indexed ) {
// No results.
t.remove();
assertResults( [], {} );
t.save( { _id:0, a:1 } );
t.save( { _id:1, a:2 } );
t.save( { _id:2, a:3 } );
// Empty query.
assertResults( [ { _id:0, a:1 }, { _id:1, a:2 }, { _id:2, a:3 } ], {} );
// Simple queries.
assertResults( [ { _id:0, a:1 } ], { a:1 } );
assertResults( [ { _id:1, a:2 } ], { a:2 } );
assertResults( [ { _id:1, a:2 }, { _id:2, a:3 } ], { a:{ $gt:1 } } );
assertResults( [ { _id:0, a:1 }, { _id:1, a:2 } ], { a:{ $lte:2 } } );
assertResults( [ { _id:0, a:1 }, { _id:2, a:3 } ], { a:{ $in:[ 1, 3 ] } } );
// Regular expression.
t.remove();
t.save( { _id:0, a:'x' } );
t.save( { _id:1, a:'yx' } );
assertResults( [ { _id:0, a:'x' } ], { a:/^x/ } );
assertResults( [ { _id:0, a:'x' }, { _id:1, a:'yx' } ], { a:/x/ } );
// Dotted field.
t.remove();
t.save( { _id:0, a:{ b:4 } } );
t.save( { _id:1, a:2 } );
assertResults( [ { _id:0, a:{ b:4 } } ], { 'a.b':4 } );
// Value within an array.
t.remove();
t.save( { _id:0, a:[ 1, 2, 3 ] } );
t.save( { _id:1, a:[ 2, 2, 3 ] } );
t.save( { _id:2, a:[ 2, 2, 2 ] } );
assertResults( [ { _id:0, a:[ 1, 2, 3 ] }, { _id:1, a:[ 2, 2, 3 ] } ], { a:3 } );
// Missing, null, $exists matching.
t.remove();
t.save( { _id:0 } );
t.save( { _id:1, a:null } );
if ( 0 ) { // SERVER-6571
t.save( { _id:2, a:undefined } );
}
t.save( { _id:3, a:0 } );
assertResults( [ { _id:0 }, { _id:1, a:null } ], { a:null } );
if ( !indexed ) { // SERVER-1160
assertResults( null, { a:{ $in:[ undefined ] } } );
}
assertResults( null, { a:{ $exists:true } } );
assertResults( null, { a:{ $exists:false } } );
// $elemMatch
t.remove();
t.save( { _id:0, a:[ 1, 2 ] } );
t.save( { _id:1, a:[ 1, 2, 3 ] } );
assertResults( [ { _id:1, a:[ 1, 2, 3 ] } ], { a:{ $elemMatch:{ $gt:1, $mod:[ 2, 1 ] } } } );
t.remove();
t.save( { _id:0, a:[ { b:1 }, { c:2 } ] } );
t.save( { _id:1, a:[ { b:1, c:2 } ] } );
assertResults( [ { _id:1, a:[ { b:1, c:2 } ] } ], { a:{ $elemMatch:{ b:1, c:2 } } } );
// $size
t.remove();
t.save( {} );
t.save( { a:null } );
t.save( { a:[] } );
t.save( { a:[ 1 ] } );
t.save( { a:[ 1, 2 ] } );
assertResults( null, { a:{ $size:0 } } );
assertResults( null, { a:{ $size:1 } } );
assertResults( null, { a:{ $size:2 } } );
// $type
t.remove();
t.save( {} );
t.save( { a:null } );
if ( 0 ) { // SERVER-6571
t.save( { a:undefined } );
}
t.save( { a:NumberInt( 1 ) } );
t.save( { a:NumberLong( 2 ) } );
t.save( { a:66.6 } );
t.save( { a:'abc' } );
t.save( { a:/xyz/ } );
t.save( { a:{ q:1 } } );
t.save( { a:true } );
t.save( { a:new Date() } );
t.save( { a:new ObjectId() } );
for( type = 1; type <= 18; ++type ) {
if ( indexed && type == 17 ) {
// SERVER-3304
continue;
}
assertResults( null, { a:{ $type:type } } );
}
// $atomic does not affect results.
t.remove();
t.save( { _id:0, a:1 } );
t.save( { _id:1, a:2 } );
t.save( { _id:2, a:3 } );
assertResults( [ { _id:0, a:1 } ], { a:1, $atomic:true } );
assertResults( [ { _id:1, a:2 } ], { a:2, $atomic:true } );
assertResults( [ { _id:1, a:2 }, { _id:2, a:3 } ], { a:{ $gt:1 }, $atomic:true } );
assertResults( [ { _id:0, a:1 }, { _id:1, a:2 } ], { a:{ $lte:2 }, $atomic:true } );
assertResults( [ { _id:0, a:1 }, { _id:2, a:3 } ], { a:{ $in:[ 1, 3 ] }, $atomic:true } );
// $and
assertResults( [ { _id:1, a:2 } ], { $and:[ { a:2 }, { _id:1 } ] } );
assertResults( [], { $and:[ { a:1 }, { _id:1 } ] } );
assertResults( [ { _id:1, a:2 }, { _id:2, a:3 } ],
{ $and:[ { $or:[ { _id:1 }, { a:3 } ] }, { $or:[ { _id:2 }, { a:2 } ] } ] } );
// $or
assertResults( [ { _id:0, a:1 }, { _id:2, a:3 } ], { $or:[ { _id:0 }, { a:3 } ] } );
}
checkMatchResults( false );
t.ensureIndex( { a:1 } );
checkMatchResults( true );
t.ensureIndex( { 'a.b':1 } );
t.ensureIndex( { 'a.c':1 } );
checkMatchResults( true );
|