diff options
author | Anton Korshunov <anton.korshunov@mongodb.com> | 2019-05-06 12:57:48 +0100 |
---|---|---|
committer | Anton Korshunov <anton.korshunov@mongodb.com> | 2019-05-06 21:20:41 +0100 |
commit | a25226e009fa1598f3077dd7972b9be3d2368785 (patch) | |
tree | 6409b1eac5ac485e52a0f777bffe1e175b5e5af1 /src/mongo/db | |
parent | ef84149f7b14e7964b2766e0b4b73160fd53fc54 (diff) | |
download | mongo-a25226e009fa1598f3077dd7972b9be3d2368785.tar.gz |
SERVER-40440 Add merge support for whenMatched: 'keepExisting'
Diffstat (limited to 'src/mongo/db')
-rw-r--r-- | src/mongo/db/pipeline/document_source_merge.cpp | 6 | ||||
-rw-r--r-- | src/mongo/db/pipeline/document_source_merge.idl | 1 | ||||
-rw-r--r-- | src/mongo/db/pipeline/document_source_merge_test.cpp | 8 |
3 files changed, 15 insertions, 0 deletions
diff --git a/src/mongo/db/pipeline/document_source_merge.cpp b/src/mongo/db/pipeline/document_source_merge.cpp index dbe77e843f7..3177affe8b1 100644 --- a/src/mongo/db/pipeline/document_source_merge.cpp +++ b/src/mongo/db/pipeline/document_source_merge.cpp @@ -65,6 +65,8 @@ constexpr auto kDefaultWhenNotMatched = WhenNotMatched::kInsert; constexpr auto kReplaceWithNewInsertMode = MergeMode{WhenMatched::kReplaceWithNew, WhenNotMatched::kInsert}; constexpr auto kMergeInsertMode = MergeMode{WhenMatched::kMerge, WhenNotMatched::kInsert}; +constexpr auto kKeepExistingInsertMode = + MergeMode{WhenMatched::kKeepExisting, WhenNotMatched::kInsert}; constexpr auto kFailInsertMode = MergeMode{WhenMatched::kFail, WhenNotMatched::kInsert}; /** @@ -136,6 +138,10 @@ const MergeStrategyDescriptorsMap& getDescriptors() { {kMergeInsertMode, {ActionType::insert, ActionType::update}, makeUpdateStrategy(true, makeUpdateTransform("$set"))}}, + {kKeepExistingInsertMode, + {kKeepExistingInsertMode, + {ActionType::insert, ActionType::update}, + makeUpdateStrategy(true, makeUpdateTransform("$setOnInsert"))}}, {kFailInsertMode, {kFailInsertMode, {ActionType::insert}, makeInsertStrategy()}}}; return mergeStrategyDescriptors; } diff --git a/src/mongo/db/pipeline/document_source_merge.idl b/src/mongo/db/pipeline/document_source_merge.idl index 0ffaa2d877b..b2c70a19ea0 100644 --- a/src/mongo/db/pipeline/document_source_merge.idl +++ b/src/mongo/db/pipeline/document_source_merge.idl @@ -45,6 +45,7 @@ enums: values: kFail: "fail" kMerge: "merge" + kKeepExisting: "keepExisting" kReplaceWithNew: "replaceWithNew" MergeWhenNotMatchedMode: diff --git a/src/mongo/db/pipeline/document_source_merge_test.cpp b/src/mongo/db/pipeline/document_source_merge_test.cpp index 8970cc33a5b..7a78fde7475 100644 --- a/src/mongo/db/pipeline/document_source_merge_test.cpp +++ b/src/mongo/db/pipeline/document_source_merge_test.cpp @@ -610,6 +610,14 @@ TEST_F(DocumentSourceMergeTest, CorrectlyHandlesWhenMatchedAndWhenNotMatchedMode << "keepExisting" << "whenNotMatched" << "insert")); + ASSERT(createMergeStage(spec)); + + spec = BSON("$merge" << BSON("into" + << "target_collection" + << "whenMatched" + << "[{$addFields: {x: 1}}]" + << "whenNotMatched" + << "insert")); ASSERT_THROWS_CODE(createMergeStage(spec), DBException, ErrorCodes::BadValue); spec = BSON("$merge" << BSON("into" |