From 28724554d4e4daa757c74f1ad5a6988ba34160f6 Mon Sep 17 00:00:00 2001 From: John Oliver Date: Thu, 22 Oct 2015 12:50:00 +0100 Subject: [PATCH] Fix #44. Allow saving of documents with an existing _id --- .../vertx/ext/mongo/impl/MongoClientImpl.java | 5 ++++- .../vertx/ext/mongo/MongoClientTestBase.java | 22 ++++++++++++------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/vertx-mongo-client/src/main/java/io/vertx/ext/mongo/impl/MongoClientImpl.java b/vertx-mongo-client/src/main/java/io/vertx/ext/mongo/impl/MongoClientImpl.java index 605ae599..8f5f17a9 100644 --- a/vertx-mongo-client/src/main/java/io/vertx/ext/mongo/impl/MongoClientImpl.java +++ b/vertx-mongo-client/src/main/java/io/vertx/ext/mongo/impl/MongoClientImpl.java @@ -100,7 +100,10 @@ public io.vertx.ext.mongo.MongoClient saveWithOptions(String collection, JsonObj JsonObject encodedDocument = encodeKeyWhenUseObjectId(document); filter.put(ID_FIELD, encodedDocument.getValue(ID_FIELD)); - coll.replaceOne(wrap(filter), encodedDocument, convertCallback(resultHandler, result -> null)); + com.mongodb.client.model.UpdateOptions updateOptions = new com.mongodb.client.model.UpdateOptions() + .upsert(true); + + coll.replaceOne(wrap(filter), encodedDocument, updateOptions, convertCallback(resultHandler, result -> null)); } return this; } diff --git a/vertx-mongo-client/src/test/java/io/vertx/ext/mongo/MongoClientTestBase.java b/vertx-mongo-client/src/test/java/io/vertx/ext/mongo/MongoClientTestBase.java index 1bce618d..83ba3f51 100644 --- a/vertx-mongo-client/src/test/java/io/vertx/ext/mongo/MongoClientTestBase.java +++ b/vertx-mongo-client/src/test/java/io/vertx/ext/mongo/MongoClientTestBase.java @@ -127,6 +127,16 @@ public void testInsertNoCollection() { await(); } + public void assertDocumentWithIdIsPresent(String collection, Object id) { + mongoClient.find(collection, + new JsonObject() + .put("_id", id), + onSuccess(result -> { + assertEquals(1, result.size()); + testComplete(); + })); + } + @Test public void testInsertNoPreexistingID() throws Exception { String collection = randomCollection(); @@ -148,8 +158,7 @@ public void testInsertPreexistingID() throws Exception { String genID = TestUtils.randomAlphaString(100); doc.put("_id", genID); mongoClient.insert(collection, doc, onSuccess(id -> { - assertNull(id); - testComplete(); + assertDocumentWithIdIsPresent(collection, genID); })); })); await(); @@ -163,8 +172,7 @@ public void testInsertPreexistingLongID() throws Exception { Long genID = TestUtils.randomLong(); doc.put("_id", genID); mongoClient.insertWithOptions(collection, doc, ACKNOWLEDGED, onSuccess(id -> { - assertNull(id); - testComplete(); + assertDocumentWithIdIsPresent(collection, genID); })); })); await(); @@ -178,8 +186,7 @@ public void testSavePreexistingLongID() throws Exception { Long genID = TestUtils.randomLong(); doc.put("_id", genID); mongoClient.saveWithOptions(collection, doc, ACKNOWLEDGED, onSuccess(id -> { - assertNull(id); - testComplete(); + assertDocumentWithIdIsPresent(collection, genID); })); })); await(); @@ -193,8 +200,7 @@ public void testInsertPreexistingObjectID() throws Exception { JsonObject genID = new JsonObject().put("id", TestUtils.randomAlphaString(100)); doc.put("_id", genID); mongoClient.insertWithOptions(collection, doc, ACKNOWLEDGED, onSuccess(id -> { - assertNull(id); - testComplete(); + assertDocumentWithIdIsPresent(collection, genID); })); })); await();