|  | 
|  | 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; | 
|  | 17 | +using System.Linq; | 
|  | 18 | +using MongoDB.Bson; | 
|  | 19 | + | 
|  | 20 | +namespace MongoDB.Driver | 
|  | 21 | +{ | 
|  | 22 | +    internal static class AggregateHelper | 
|  | 23 | +    { | 
|  | 24 | +        public static RenderedPipelineDefinition<TResult> RenderAggregatePipeline<TDocument, TResult>(PipelineDefinition<TDocument, TResult> pipeline, RenderArgs<TDocument> renderArgs, out bool isAggregateToCollection) | 
|  | 25 | +        { | 
|  | 26 | +            var renderedPipeline = pipeline.Render(renderArgs); | 
|  | 27 | + | 
|  | 28 | +            var lastStage = renderedPipeline.Documents.LastOrDefault(); | 
|  | 29 | +            var lastStageName = lastStage?.GetElement(0).Name; | 
|  | 30 | +            isAggregateToCollection = lastStageName == "$out" || lastStageName == "$merge"; | 
|  | 31 | + | 
|  | 32 | +            return renderedPipeline; | 
|  | 33 | +        } | 
|  | 34 | + | 
|  | 35 | +        public static CollectionNamespace GetOutCollection(BsonDocument outStage, DatabaseNamespace defaultDatabaseNamespace) | 
|  | 36 | +        { | 
|  | 37 | +            var stageName = outStage.GetElement(0).Name; | 
|  | 38 | +            switch (stageName) | 
|  | 39 | +            { | 
|  | 40 | +                case "$out": | 
|  | 41 | +                    { | 
|  | 42 | +                        var outValue = outStage[0]; | 
|  | 43 | +                        DatabaseNamespace outputDatabaseNamespace; | 
|  | 44 | +                        string outputCollectionName; | 
|  | 45 | +                        if (outValue.IsString) | 
|  | 46 | +                        { | 
|  | 47 | +                            outputDatabaseNamespace = defaultDatabaseNamespace; | 
|  | 48 | +                            outputCollectionName = outValue.AsString; | 
|  | 49 | +                        } | 
|  | 50 | +                        else | 
|  | 51 | +                        { | 
|  | 52 | +                            outputDatabaseNamespace = new DatabaseNamespace(outValue["db"].AsString); | 
|  | 53 | +                            outputCollectionName = outValue["coll"].AsString; | 
|  | 54 | +                        } | 
|  | 55 | +                        return new CollectionNamespace(outputDatabaseNamespace, outputCollectionName); | 
|  | 56 | +                    } | 
|  | 57 | +                case "$merge": | 
|  | 58 | +                    { | 
|  | 59 | +                        var mergeArguments = outStage[0]; | 
|  | 60 | +                        DatabaseNamespace outputDatabaseNamespace; | 
|  | 61 | +                        string outputCollectionName; | 
|  | 62 | +                        if (mergeArguments.IsString) | 
|  | 63 | +                        { | 
|  | 64 | +                            outputDatabaseNamespace = defaultDatabaseNamespace; | 
|  | 65 | +                            outputCollectionName = mergeArguments.AsString; | 
|  | 66 | +                        } | 
|  | 67 | +                        else | 
|  | 68 | +                        { | 
|  | 69 | +                            var into = mergeArguments.AsBsonDocument["into"]; | 
|  | 70 | +                            if (into.IsString) | 
|  | 71 | +                            { | 
|  | 72 | +                                outputDatabaseNamespace = defaultDatabaseNamespace; | 
|  | 73 | +                                outputCollectionName = into.AsString; | 
|  | 74 | +                            } | 
|  | 75 | +                            else | 
|  | 76 | +                            { | 
|  | 77 | +                                if (into.AsBsonDocument.Contains("db")) | 
|  | 78 | +                                { | 
|  | 79 | +                                    outputDatabaseNamespace = new DatabaseNamespace(into["db"].AsString); | 
|  | 80 | +                                } | 
|  | 81 | +                                else | 
|  | 82 | +                                { | 
|  | 83 | +                                    outputDatabaseNamespace = defaultDatabaseNamespace; | 
|  | 84 | +                                } | 
|  | 85 | +                                outputCollectionName = into["coll"].AsString; | 
|  | 86 | +                            } | 
|  | 87 | +                        } | 
|  | 88 | +                        return new CollectionNamespace(outputDatabaseNamespace, outputCollectionName); | 
|  | 89 | +                    } | 
|  | 90 | +                default: | 
|  | 91 | +                    throw new ArgumentException($"Unexpected stage name: {stageName}."); | 
|  | 92 | +            } | 
|  | 93 | +        } | 
|  | 94 | +    } | 
|  | 95 | +} | 
|  | 96 | + | 
0 commit comments