0

I want to get bulk write metrics from Mongo server, like count of insertions, deletions, modifications, but the following methods always return 0.

 BulkWriteResult.getInsertedCount()
 BulkWriteResult.getMatchedCount()
 BulkWriteResult.getDeletedCount()
 BulkWriteResult.getModifiedCount()

this is my code snippet-

 List<ReplaceOneModel<Document>> request = new ArrayList<>(Arrays.asList( new ReplaceOneModel<>(
            new Document("_id", "133499"),
            document,
            new ReplaceOptions().upsert(true))));

 BulkWriteResult writeResult = mongoCollection.bulkWrite(request);
 writeResult.getInsertedCount(),
 writeResult.getMatchedCount(),
 writeResult.getDeletedCount(),
 writeResult.getModifiedCount());

All bulk writes are executed successfully, but the counts of insertions, modifications, always return 0.

3
  • That is request a 'replace' operation, so it will only have an effect if there is a document in the collection with an _id field whose value is the string "133499". Is there such a document? Commented Aug 2, 2021 at 6:48
  • According to the docs it only upserts if you explicitly set upsert to true. Have you tried passing options to that constructor? Commented Aug 2, 2021 at 6:59
  • yeah i have edited the code in the question, i have run my code with upsert set to true, upserts are working fine, but insertedCount() always returning 0 Commented Aug 2, 2021 at 7:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.