vert-x3 / vertx-mongo-client

Mongo Client for Eclipse Vert.x
http://vertx.io
Apache License 2.0
60 stars 99 forks source link

bulkWrite works only for the last item of bulkOperations #199

Open leolux opened 5 years ago

leolux commented 5 years ago

The method bulkWrite() accepts a list of BulkOperations. My finding is that only the last item of the list gets actually written to mongoDB and everything else is kind of ignored.

The callback of bulkWrite() returns successful without any error which is a bit strange.

Note: I use mongoDB v3.4.7

leolux commented 5 years ago

There is currently no unit test which tests a bulk write with multiple replace operations. Therefore I have created a quick test to verify: https://pastebin.com/SAieGLQ5

This is the only test that fails.

kostya05983 commented 5 years ago

Hello, @leolux , Thanks for submitting, I think you may submit it to mongodb driver. We use an async-mongodb-driver. I check that vertx-mongo-driver passes all operations to mongo async client and get a result from it. Can you check that you get the same result with mongodb-async driver? You can find it here https://mvnrepository.com/artifact/org.mongodb/mongodb-driver-async

leolux commented 5 years ago

I can't get the first two lines of mongodb-driver-async t to work:

MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("test");

Both lines are marked as deprecated and the error is: No server chosen by com.mongodb.async.client.ClientSessionHelper

I tried to follow this guide: https://mongodb.github.io/mongo-java-driver/3.10/driver-async/getting-started/quick-start/#connect-to-a-standalone-mongodb-instance

Maybe someone else has more luck testing bulk replaces with mongodb-driver-async.

karianna commented 4 years ago

There is currently no unit test which tests a bulk write with multiple replace operations. Therefore I have created a quick test to verify: https://pastebin.com/SAieGLQ5

This is the only test that fails.

We're not sure this test is surfacing a bug. The test inserts a single document and then attempted to do 2 bulk operations, one of the inserted doc and another on a doc that doesn't exist, which is why we only get back a modifiedCount of 1

karianna commented 4 years ago

The following test shows bulk operations 'working'

@Test
public void testBulkOperation_multiReplaces() {
  String collection = randomCollection();
  insertDocs(mongoClient, collection, 2, onSuccess(v -> {
    JsonObject filter1 = new JsonObject().put("foo", "bar0");
    JsonObject replace1 = new JsonObject().put("foo", "replaced");
    BulkOperation bulkReplace1 = BulkOperation.createReplace(filter1, replace1);
    JsonObject filter2 = new JsonObject().put("foo", "bar1");
    JsonObject replace2 = new JsonObject().put("foo", "bar");
    BulkOperation bulkReplace2 = BulkOperation.createReplace(filter2, replace2);
    mongoClient.bulkWrite(collection, Arrays.asList(bulkReplace1, bulkReplace2), onSuccess(bulkResult -> {
      assertEquals(0, bulkResult.getInsertedCount());
      assertEquals(2, bulkResult.getModifiedCount());
      assertEquals(0, bulkResult.getDeletedCount());
      assertEquals(2, bulkResult.getMatchedCount());
      assertEquals(0, bulkResult.getUpserts().size());
      mongoClient.find(collection, new JsonObject(), onSuccess(docs -> {
        assertEquals(2, docs.size());
        JsonObject foundDoc = docs.get(0);
        assertEquals("replaced", foundDoc.getString("foo"));
        assertNull(foundDoc.getInteger("num"));
        foundDoc = docs.get(1);
        assertEquals("bar", foundDoc.getString("foo"));
        assertNull(foundDoc.getInteger("num"));
        testComplete();
      }));
    }));
  }));
  await();
}
karianna commented 4 years ago

@leolux Can you confirm that this works for you?

leolux commented 4 years ago

Thank you for improving the testcase. I like it to setup a similar testcase using the mongodb-driver-async driver because that is the driver where I was facing the issue initially. I need a couple of days for this because I got too much going on