Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.morphia.aggregation.codecs.stages;

import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;

Expand Down Expand Up @@ -73,9 +74,17 @@ protected void encodeStage(BsonWriter writer, Merge merge, EncoderContext encode
}
}

/**
* Writes the MQL spelling of a driver merge option: {@code KEEP_EXISTING} is {@code keepExisting}.
*/
private void writeEnum(BsonWriter writer, String name, Enum<?> value) {
if (value != null) {
writer.writeString(name, value.name().toLowerCase());
String[] words = value.name().toLowerCase(Locale.ROOT).split("_");
StringBuilder mql = new StringBuilder(words[0]);
for (int i = 1; i < words.length; i++) {
mql.append(Character.toUpperCase(words[i].charAt(0))).append(words[i].substring(1));
}
writer.writeString(name, mql.toString());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@

import java.time.LocalDate;
import java.time.Month;
import java.util.ArrayList;
import java.util.List;

import dev.morphia.test.TemplatedTestBase;
import dev.morphia.test.util.ActionTestOptions;

import org.bson.Document;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static com.mongodb.client.model.MergeOptions.WhenMatched.FAIL;
import static com.mongodb.client.model.MergeOptions.WhenMatched.KEEP_EXISTING;
import static com.mongodb.client.model.MergeOptions.WhenMatched.MERGE;
import static com.mongodb.client.model.MergeOptions.WhenMatched.REPLACE;
import static com.mongodb.client.model.MergeOptions.WhenNotMatched.INSERT;
import static dev.morphia.aggregation.AggregationOptions.aggregationOptions;
import static dev.morphia.aggregation.expressions.AccumulatorExpressions.push;
import static dev.morphia.aggregation.expressions.AccumulatorExpressions.sum;
import static dev.morphia.aggregation.expressions.DateExpressions.dateToString;
Expand Down Expand Up @@ -138,4 +144,21 @@ public void testExample8() {
// merging in to another db complicates the test infra and doesn't really
// provide much value as another test
}

@Test
@DisplayName("whenMatched: keepExisting keeps the target's document")
public void keepExisting() {
getDatabase().getCollection("mergeSource").insertMany(List.of(
new Document("_id", 1).append("value", "new"),
new Document("_id", 2).append("value", "new")));
getDatabase().getCollection("mergeTarget").insertOne(new Document("_id", 1).append("value", "old"));

getDs().aggregate(aggregationOptions().collection("mergeSource"))
.pipeline(merge("mergeTarget").on("_id").whenMatched(KEEP_EXISTING).whenNotMatched(INSERT))
.toList();

Assertions.assertEquals(
List.of(new Document("_id", 1).append("value", "old"), new Document("_id", 2).append("value", "new")),
getDatabase().getCollection("mergeTarget").find().sort(new Document("_id", 1)).into(new ArrayList<>()));
}
}
Loading