File tree Expand file tree Collapse file tree 3 files changed +61
-0
lines changed
src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Optimizers
tests/MongoDB.Driver.Tests/Linq/Linq3ImplementationTests Expand file tree Collapse file tree 3 files changed +61
-0
lines changed Original file line number Diff line number Diff line change 1313* limitations under the License.
1414*/
1515
16+ using MongoDB . Bson ;
1617using MongoDB . Driver . Linq . Linq3Implementation . Ast . Expressions ;
18+ using MongoDB . Driver . Linq . Linq3Implementation . Ast . Filters ;
19+ using MongoDB . Driver . Linq . Linq3Implementation . Ast . Stages ;
1720using MongoDB . Driver . Linq . Linq3Implementation . Ast . Visitors ;
1821
1922namespace MongoDB . Driver . Linq . Linq3Implementation . Ast . Optimizers
@@ -105,6 +108,21 @@ static AstExpression UltimateGetFieldInput(AstGetFieldExpression getField)
105108 }
106109 }
107110
111+ public override AstNode VisitMatchStage ( AstMatchStage node )
112+ {
113+ node = ( AstMatchStage ) base . VisitMatchStage ( node ) ;
114+
115+ if ( node . Filter is AstExprFilter exprFilter &&
116+ exprFilter . Expression is AstConstantExpression constantExpression &&
117+ constantExpression . Value is BsonBoolean booleanValue )
118+ {
119+ var simpleFilter = booleanValue . Value ? AstFilter . MatchesEverything ( ) : AstFilter . MatchesNothing ( ) ;
120+ return AstStage . Match ( simpleFilter ) ;
121+ }
122+
123+ return node ;
124+ }
125+
108126 public override AstNode VisitUnaryExpression ( AstUnaryExpression node )
109127 {
110128 // { $first : <arg> } => { $arrayElemAt : [<arg>, 0] } (or -1 for $last)
Original file line number Diff line number Diff line change 1+ /* Copyright 2010-present MongoDB Inc.
2+ *
3+ * Licensed under the Apache License, Version 2.0 (the "License");
4+ * you may not use this file except in compliance with the License.
5+ * You may obtain a copy of the License at
6+ *
7+ * http://www.apache.org/licenses/LICENSE-2.0
8+ *
9+ * Unless required by applicable law or agreed to in writing, software
10+ * distributed under the License is distributed on an "AS IS" BASIS,
11+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ * See the License for the specific language governing permissions and
13+ * limitations under the License.
14+ */
15+
16+ using System . Linq ;
17+ using MongoDB . Bson ;
18+ using Xunit ;
19+
20+ namespace MongoDB . Driver . Tests . Linq . Linq3ImplementationTests . Jira
21+ {
22+ public class CSharp4116Tests : Linq3IntegrationTest
23+ {
24+ [ Theory ]
25+ [ InlineData ( false , "{ $match : { _id : { $type : -1 } } }" ) ]
26+ [ InlineData ( true , "{ $match : { } }" ) ]
27+ public void Optimize_match_with_expr ( bool value , string expectedStage )
28+ {
29+ var collection = GetCollection < BsonDocument > ( ) ;
30+ var queryable = collection . AsQueryable ( )
31+ . Where ( x => value ) ;
32+
33+ var stages = Translate ( collection , queryable ) ;
34+
35+ AssertStages ( stages , expectedStage ) ;
36+ }
37+ }
38+ }
Original file line number Diff line number Diff line change @@ -25,6 +25,11 @@ namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests
2525{
2626 public abstract class Linq3IntegrationTest
2727 {
28+ protected void AssertStages ( IEnumerable < BsonDocument > stages , params string [ ] expectedStages )
29+ {
30+ AssertStages ( stages , ( IEnumerable < string > ) expectedStages ) ;
31+ }
32+
2833 protected void AssertStages ( IEnumerable < BsonDocument > stages , IEnumerable < string > expectedStages )
2934 {
3035 stages . Should ( ) . Equal ( expectedStages . Select ( json => BsonDocument . Parse ( json ) ) ) ;
You can’t perform that action at this time.
0 commit comments