Skip to content

Commit

Permalink
Fix replace with empty document (#227) (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
bwaldvogel committed Mar 19, 2024
1 parent 9e8ec5e commit 0b604ed
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
Expand Up @@ -345,14 +345,14 @@ private Document calculateUpdateDocument(Document oldDocument, Document update,
newDocument.put(getIdField(), oldDocument.get(getIdField()));
}

if (numStartsWithDollar == update.keySet().size()) {
if (numStartsWithDollar == 0) {
applyUpdate(newDocument, update);
} else if (numStartsWithDollar == update.keySet().size()) {
validateUpdateQuery(update);
oldDocument.cloneInto(newDocument);
for (String key : update.keySet()) {
modifyField(newDocument, key, update, arrayFilters, matchPos, isUpsert);
}
} else if (numStartsWithDollar == 0) {
applyUpdate(newDocument, update);
} else {
throw new MongoServerException("illegal update: " + update);
}
Expand Down
Expand Up @@ -3689,6 +3689,17 @@ void testReplaceOneUpsertsWithGeneratedId() throws Exception {
.isInstanceOf(ObjectId.class);
}

// https://github.com/bwaldvogel/mongo-java-server/issues/227
@Test
void testReplaceWithEmptyDocument() throws Exception {
collection.insertOne(json("_id: 'myId', value: 'test'"));

collection.replaceOne(json("_id: 'myId'"), json(""));

assertThat(collection.find())
.containsExactly(json("_id: 'myId'"));
}

// https://github.com/bwaldvogel/mongo-java-server/issues/41
@Test
void testBulkUpsert() throws Exception {
Expand Down

0 comments on commit 0b604ed

Please sign in to comment.