From cbf584b6b0d7130077d0928e44a1baf777968fe1 Mon Sep 17 00:00:00 2001 From: Andrea Di Cesare Date: Fri, 19 Apr 2024 12:29:13 +0200 Subject: [PATCH] :bug: Fix bulk post with writeMode=update does not replace the matching documents but just updates the passed properties https://github.com/SoftInstigate/restheart/issues/499 --- .../org/restheart/mongodb/db/DbUtils.java | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/mongodb/src/main/java/org/restheart/mongodb/db/DbUtils.java b/mongodb/src/main/java/org/restheart/mongodb/db/DbUtils.java index 3f4bb0f19..e8b31f317 100644 --- a/mongodb/src/main/java/org/restheart/mongodb/db/DbUtils.java +++ b/mongodb/src/main/java/org/restheart/mongodb/db/DbUtils.java @@ -20,25 +20,12 @@ */ package org.restheart.mongodb.db; -import com.mongodb.client.ClientSession; -import com.mongodb.client.MongoCollection; -import com.mongodb.client.model.BulkWriteOptions; -import static com.mongodb.client.model.Filters.and; -import static com.mongodb.client.model.Filters.eq; -import static com.mongodb.client.model.Filters.exists; -import com.mongodb.client.model.FindOneAndReplaceOptions; -import com.mongodb.client.model.FindOneAndUpdateOptions; -import com.mongodb.client.model.InsertOneModel; -import com.mongodb.client.model.ReplaceOptions; -import com.mongodb.client.model.ReturnDocument; -import com.mongodb.client.model.UpdateOneModel; -import com.mongodb.client.model.UpdateOptions; -import com.mongodb.client.model.WriteModel; import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.Optional; import java.util.stream.Collectors; + import org.bson.BsonArray; import org.bson.BsonBoolean; import org.bson.BsonDateTime; @@ -51,11 +38,26 @@ import org.bson.types.ObjectId; import org.restheart.exchange.ExchangeKeys.METHOD; import org.restheart.exchange.ExchangeKeys.WRITE_MODE; -import org.restheart.utils.HttpStatus; import org.restheart.utils.BsonUtils; +import org.restheart.utils.HttpStatus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.mongodb.client.ClientSession; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.model.BulkWriteOptions; +import static com.mongodb.client.model.Filters.and; +import static com.mongodb.client.model.Filters.eq; +import static com.mongodb.client.model.Filters.exists; +import com.mongodb.client.model.FindOneAndReplaceOptions; +import com.mongodb.client.model.FindOneAndUpdateOptions; +import com.mongodb.client.model.InsertOneModel; +import com.mongodb.client.model.ReplaceOneModel; +import com.mongodb.client.model.ReplaceOptions; +import com.mongodb.client.model.ReturnDocument; +import com.mongodb.client.model.UpdateOptions; +import com.mongodb.client.model.WriteModel; + /** * * @author Andrea Di Cesare {@literal } @@ -423,8 +425,8 @@ static List> getBulkWriteModel( } switch(writeMode) { - case UPSERT -> updates.add(new UpdateOneModel<>(_filter, getUpdateDocument(document), new UpdateOptions().upsert(true))); - case UPDATE -> updates.add(new UpdateOneModel<>(_filter, getUpdateDocument(document), new UpdateOptions().upsert(false))); + case UPSERT -> updates.add(new ReplaceOneModel<>(_filter, document, new ReplaceOptions().upsert(true))); + case UPDATE -> updates.add(new ReplaceOneModel<>(_filter, document, new ReplaceOptions().upsert(false))); case INSERT -> updates.add(new InsertOneModel<>(document)); } });