|
| 1 | +/* Copyright 2019-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.Collections.Generic; |
| 17 | +using MongoDB.Bson; |
| 18 | +using MongoDB.Bson.Serialization; |
| 19 | + |
| 20 | +namespace MongoDB.Driver |
| 21 | +{ |
| 22 | + /// <summary> |
| 23 | + /// The behavior of $merge is a result document and an existing document in the collection |
| 24 | + /// have the same value for the specified on field(s). |
| 25 | + /// </summary> |
| 26 | + public enum MergeStageWhenMatched |
| 27 | + { |
| 28 | + /// <summary> |
| 29 | + /// Replace the existing document in the output collection with the matching results document. |
| 30 | + /// </summary> |
| 31 | + Replace, |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Keep the existing document in the output collection. |
| 35 | + /// </summary> |
| 36 | + KeepExisting, |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Merge the matching documents (similar to the $mergeObjects operator). |
| 40 | + /// </summary> |
| 41 | + Merge, |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Stop and fail the aggregation. Any changes to the output collection from previous documents are not reverted. |
| 45 | + /// </summary> |
| 46 | + Fail, |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Use an aggregation pipeline to update the document in the collection. |
| 50 | + /// </summary> |
| 51 | + Pipeline |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// The behavior of $merge if a result document does not match an existing document in the output collection. |
| 56 | + /// </summary> |
| 57 | + public enum MergeStageWhenNotMatched |
| 58 | + { |
| 59 | + /// <summary> |
| 60 | + /// Insert the document into the output collection. |
| 61 | + /// </summary> |
| 62 | + Insert, |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Discard the document; i.e. $merge does not insert the document into the output collection. |
| 66 | + /// </summary> |
| 67 | + Discard, |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Stop and fail the aggregation operation. Any changes to the output collection from previous documents are not reverted. |
| 71 | + /// </summary> |
| 72 | + Fail |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Options for the $merge aggregation pipeline stage. |
| 77 | + /// </summary> |
| 78 | + /// <typeparam name="TOutput">The type of the output documents.</typeparam> |
| 79 | + public class MergeStageOptions<TOutput> |
| 80 | + { |
| 81 | + // private fields |
| 82 | + private BsonDocument _letVariables; |
| 83 | + private IReadOnlyList<string> _onFieldNames; |
| 84 | + private IBsonSerializer<TOutput> _outputSerializer; |
| 85 | + private MergeStageWhenMatched? _whenMatched; |
| 86 | + private PipelineDefinition<TOutput, TOutput> _whenMatchedPipeline; |
| 87 | + private MergeStageWhenNotMatched? _whenNotMatched; |
| 88 | + |
| 89 | + // public properties |
| 90 | + /// <summary> |
| 91 | + /// Specifies variables accessible for use in the WhenMatchedPipeline. |
| 92 | + /// </summary> |
| 93 | + public BsonDocument LetVariables |
| 94 | + { |
| 95 | + get => _letVariables; |
| 96 | + set => _letVariables = value; |
| 97 | + } |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// Field or fields that act as a unique identifier for a document. The identifier determines if a results |
| 101 | + /// document matches an already existing document in the output collection. |
| 102 | + /// </summary> |
| 103 | + public IReadOnlyList<string> OnFieldNames |
| 104 | + { |
| 105 | + get => _onFieldNames; |
| 106 | + set => _onFieldNames = value; |
| 107 | + } |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// The output serializer. |
| 111 | + /// </summary> |
| 112 | + public IBsonSerializer<TOutput> OutputSerializer |
| 113 | + { |
| 114 | + get => _outputSerializer; |
| 115 | + set => _outputSerializer = value; |
| 116 | + } |
| 117 | + |
| 118 | + /// <summary> |
| 119 | + /// The behavior of $merge if a result document and an existing document in the collectoin have the |
| 120 | + /// same value for the specified on field(s). |
| 121 | + /// </summary> |
| 122 | + public MergeStageWhenMatched? WhenMatched |
| 123 | + { |
| 124 | + get => _whenMatched; |
| 125 | + set => _whenMatched = value; |
| 126 | + } |
| 127 | + |
| 128 | + /// <summary> |
| 129 | + /// An aggregation pipeline to update the document in the collection. |
| 130 | + /// Used when WhenMatched is Pipeline. |
| 131 | + /// </summary> |
| 132 | + public PipelineDefinition<TOutput, TOutput> WhenMatchedPipeline |
| 133 | + { |
| 134 | + get => _whenMatchedPipeline; |
| 135 | + set => _whenMatchedPipeline = value; |
| 136 | + } |
| 137 | + |
| 138 | + /// <summary> |
| 139 | + /// The behavior of $merge if a result document does not match an existing document in the output collection. |
| 140 | + /// </summary> |
| 141 | + public MergeStageWhenNotMatched? WhenNotMatched |
| 142 | + { |
| 143 | + get => _whenNotMatched; |
| 144 | + set => _whenNotMatched = value; |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments