private AstPipeline OptimizeGroupStage(AstPipeline pipeline, int i, AstGroupStage groupStage) { try { if (IsOptimizableGroupStage(groupStage)) { var followingStages = GetFollowingStagesToOptimize(pipeline, i + 1); if (followingStages == null) { return(pipeline); } var mappings = OptimizeGroupAndFollowingStages(groupStage, followingStages); if (mappings.Length > 0) { return((AstPipeline)AstNodeReplacer.Replace(pipeline, mappings)); } } } catch (UnableToRemoveReferenceToElementsException) { // wasn't able to optimize away all references to _elements } return(pipeline);
static bool IsOptimizableGroupStage(AstGroupStage groupStage) { // { $group : { _id : ?, _elements : { $push : "$$ROOT" } } } if (groupStage.Fields.Count == 1) { var field = groupStage.Fields[0]; if (field.Path == "_elements" && field.Value.Operator == AstAccumulatorOperator.Push && field.Value.Arg is AstVarExpression varExpression && varExpression.Name == "ROOT") { return(true); } } return(false); }