use of com.mongodb.client.model.BulkWriteOptions in project mongo-java-driver by mongodb.
the class UnifiedCrudHelper method executeBulkWrite.
OperationResult executeBulkWrite(final BsonDocument operation) {
MongoCollection<BsonDocument> collection = entities.getCollection(operation.getString("object").getValue());
BsonDocument arguments = operation.getDocument("arguments");
List<WriteModel<BsonDocument>> requests = arguments.getArray("requests").stream().map(value -> toWriteModel(value.asDocument())).collect(toList());
BulkWriteOptions options = new BulkWriteOptions();
for (Map.Entry<String, BsonValue> cur : arguments.entrySet()) {
switch(cur.getKey()) {
case "requests":
break;
case "ordered":
options.ordered(cur.getValue().asBoolean().getValue());
break;
default:
throw new UnsupportedOperationException("Unsupported argument: " + cur.getKey());
}
}
return resultOf(() -> toExpected(collection.bulkWrite(requests, options)));
}
use of com.mongodb.client.model.BulkWriteOptions in project core-ng-project by neowu.
the class MongoCollectionImpl method bulkReplace.
@Override
public void bulkReplace(List<T> entities) {
StopWatch watch = new StopWatch();
if (entities == null || entities.isEmpty())
throw Exceptions.error("entities must not be empty");
for (T entity : entities) {
validator.validate(entity);
}
try {
List<ReplaceOneModel<T>> models = new ArrayList<>(entities.size());
for (T entity : entities) {
Object id = mongo.codecs.id(entity);
if (id == null)
throw Exceptions.error("entity must have id, entityClass={}", entityClass.getCanonicalName());
models.add(new ReplaceOneModel<>(Filters.eq("_id", id), entity, new UpdateOptions().upsert(true)));
}
collection().bulkWrite(models, new BulkWriteOptions().ordered(false));
} finally {
long elapsedTime = watch.elapsedTime();
ActionLogContext.track("mongoDB", elapsedTime, 0, entities.size());
logger.debug("bulkReplace, collection={}, size={}, elapsedTime={}", collectionName, entities.size(), elapsedTime);
checkSlowOperation(elapsedTime);
}
}
Aggregations