In the earlier versions of MongoDB Java drivers , to run a query and do unordered bulk upsert on the result all we had do was :
BulkWriteOperation bulk = dbCollection.initializeUnorderedBulkOperation();
bulk.find(searchQuery).upsert().update(new BasicDBObject("$set", getDbObjectModel()));
List<WriteModel<Document>> documentList = new ArrayList<>();
collection.bulkWrite(documentList, new BulkWriteOptions().ordered(false));
You can still use all of the functionality, it's just that BulkWrites now have a different syntax:
MongoCollection<Document> collection = db.getCollection("sample"); List<WriteModel<Document>> updates = Arrays.<WriteModel<Document>>asList( new UpdateOneModel<Document>( new Document(), // find part new Document("$set",1), // update part new UpdateOptions().upsert(true) // options like upsert ) ); BulkWriteResult bulkWriteResult = collection.bulkWrite(updates);
Takes some getting used to, but it's basically just building "Lists" with all the same syntax as elsewhere. I guess that's the main reason for the change.