@@ -207,10 +207,28 @@ private IMongoQuery BuildArrayLengthQuery(Expression variableExpression, Express
207
207
return null ;
208
208
}
209
209
210
+ private IMongoQuery BuildBooleanQuery ( bool value )
211
+ {
212
+ if ( value )
213
+ {
214
+ return new QueryDocument ( ) ; // empty query matches all documents
215
+ }
216
+ else
217
+ {
218
+ return _queryBuilder . Type ( "_id" , ( BsonType ) ( - 1 ) ) ; // matches no documents (and uses _id index when used at top level)
219
+ }
220
+ }
221
+
210
222
private IMongoQuery BuildBooleanQuery ( Expression expression )
211
223
{
212
224
if ( expression . Type == typeof ( bool ) )
213
225
{
226
+ var constantExpression = expression as ConstantExpression ;
227
+ if ( constantExpression != null )
228
+ {
229
+ return BuildBooleanQuery ( ( bool ) constantExpression . Value ) ;
230
+ }
231
+
214
232
var serializationInfo = _serializationInfoHelper . GetSerializationInfo ( expression ) ;
215
233
return new QueryDocument ( serializationInfo . ElementName , true ) ;
216
234
}
@@ -336,9 +354,7 @@ private IMongoQuery BuildConstantQuery(ConstantExpression constantExpression)
336
354
var value = constantExpression . Value ;
337
355
if ( value != null && value . GetType ( ) == typeof ( bool ) )
338
356
{
339
- // simulate true or false with a tautology or a reverse tautology
340
- // the particular reverse tautology chosen has the nice property that it uses the index to return no results quickly
341
- return new QueryDocument ( "_id" , new BsonDocument ( "$exists" , ( bool ) value ) ) ;
357
+ return BuildBooleanQuery ( ( bool ) value ) ;
342
358
}
343
359
344
360
return null ;
@@ -858,7 +874,7 @@ private IMongoQuery BuildStringIndexOfQuery(Expression variableExpression, Expre
858
874
if ( index >= startIndex + count )
859
875
{
860
876
// index is outside of the substring so no match is possible
861
- return _queryBuilder . NotExists ( "_id" ) ; // matches no documents
877
+ return BuildBooleanQuery ( false ) ;
862
878
}
863
879
else
864
880
{
@@ -892,7 +908,7 @@ private IMongoQuery BuildStringIndexOfQuery(Expression variableExpression, Expre
892
908
if ( unescapedLength > startIndex + count - index )
893
909
{
894
910
// substring isn't long enough to match
895
- return _queryBuilder . NotExists ( "_id" ) ; // matches no documents
911
+ return BuildBooleanQuery ( false ) ;
896
912
}
897
913
else
898
914
{
@@ -1099,12 +1115,12 @@ private IMongoQuery BuildStringCaseInsensitiveComparisonQuery(Expression variabl
1099
1115
if ( operatorType == ExpressionType . Equal )
1100
1116
{
1101
1117
// == "mismatched case" matches no documents
1102
- return _queryBuilder . NotExists ( "_id" ) ;
1118
+ return BuildBooleanQuery ( false ) ;
1103
1119
}
1104
1120
else
1105
1121
{
1106
1122
// != "mismatched case" matches all documents
1107
- return new QueryDocument ( ) ;
1123
+ return BuildBooleanQuery ( true ) ;
1108
1124
}
1109
1125
}
1110
1126
}
@@ -1265,7 +1281,7 @@ private IMongoQuery BuildTypeComparisonQuery(Expression variableExpression, Expr
1265
1281
var discriminator = discriminatorConvention . GetDiscriminator ( nominalType , actualType ) ;
1266
1282
if ( discriminator == null )
1267
1283
{
1268
- return new QueryDocument ( ) ; // matches everything
1284
+ return BuildBooleanQuery ( true ) ;
1269
1285
}
1270
1286
1271
1287
if ( discriminator . IsBsonArray )
@@ -1296,7 +1312,7 @@ private IMongoQuery BuildTypeIsQuery(TypeBinaryExpression typeBinaryExpression)
1296
1312
var discriminator = discriminatorConvention . GetDiscriminator ( nominalType , actualType ) ;
1297
1313
if ( discriminator == null )
1298
1314
{
1299
- return new QueryDocument ( ) ; // matches everything
1315
+ return BuildBooleanQuery ( true ) ;
1300
1316
}
1301
1317
1302
1318
if ( discriminator . IsBsonArray )
0 commit comments