Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bulkWrite works only for the last item of bulkOperations #199

Open
leolux opened this issue Jul 16, 2019 · 7 comments
Open

bulkWrite works only for the last item of bulkOperations #199

leolux opened this issue Jul 16, 2019 · 7 comments

Comments

@leolux
Copy link
Contributor

leolux commented Jul 16, 2019

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

@karianna karianna added this to the 4.0.0 milestone Jul 16, 2019
@karianna karianna added the bug label Jul 16, 2019
@leolux
Copy link
Contributor Author

leolux commented Jul 16, 2019

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
Copy link
Contributor

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
Copy link
Contributor Author

leolux commented Aug 9, 2019

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 karianna removed this from the 4.0.0 milestone May 12, 2020
@karianna
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

@leolux Can you confirm that this works for you?

@leolux
Copy link
Contributor Author

leolux commented May 17, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

4 participants