How To Use BulkWriteUpsert and UpdateRequest JAVA Driver to Update Bulk Documents?

295 views
Skip to first unread message

Vashitva Srivastava

unread,
May 23, 2016, 6:01:46 PM5/23/16
to mongodb-user
I have To Update 10,000 Documents how to do that using BulkWriteUpsert and UpdateRequest. each Document have only custom _id being unique.

Wan Bachtiar

unread,
Jun 2, 2016, 1:59:16 AM6/2/16
to mongodb-user

Hi Vashitva,

Please see an example of Bulk Operations in the MongoDB Java Driver QuickTour page. For example, you can do ordered bulk write to insert, update and delete as below:

List<WriteModel<Document>> writes = new ArrayList<WriteModel<Document>>();
writes.add(new InsertOneModel<Document>(new Document("_id", 6)));
writes.add(new UpdateOneModel<Document>(new Document("_id", 1), new Document("$set", new Document("x", 2))));
writes.add(new DeleteOneModel<Document>(new Document("_id", 2)));
collection.bulkWrite(writes);

The snippet code above is for MongoDB v3.2 and MongoDB Java Driver v3.2.x.

If you are facing a specific issue using bulk operations, could you post relevant details of:

  • MongoDB server version
  • MongoDB Java driver version
  • The Java code snippet containing the bulk operations
  • The error/problem that you are seeing

See also BulkWriteOperation class.

Regards,

Wan.

Vashitva Srivastava

unread,
Jun 20, 2016, 3:03:57 AM6/20/16
to mongodb-user
Thanks Wan, It helped me alot.
Reply all
Reply to author
Forward
0 new messages