Search in sources :

Example 6 with BulkWriteOptions

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)));
}
Also used : InsertManyResult(com.mongodb.client.result.InsertManyResult) ReplaceOptions(com.mongodb.client.model.ReplaceOptions) DeleteOptions(com.mongodb.client.model.DeleteOptions) BsonDocumentWriter(org.bson.BsonDocumentWriter) MongoDatabase(com.mongodb.client.MongoDatabase) DeleteManyModel(com.mongodb.client.model.DeleteManyModel) WriteModel(com.mongodb.client.model.WriteModel) ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) BsonValue(org.bson.BsonValue) BsonElement(org.bson.BsonElement) UpdateResult(com.mongodb.client.result.UpdateResult) Arrays.asList(java.util.Arrays.asList) BsonArray(org.bson.BsonArray) Map(java.util.Map) ReadConcern(com.mongodb.ReadConcern) ListCollectionsIterable(com.mongodb.client.ListCollectionsIterable) DeleteOneModel(com.mongodb.client.model.DeleteOneModel) UpdateOptions(com.mongodb.client.model.UpdateOptions) EncoderContext(org.bson.codecs.EncoderContext) UpdateManyModel(com.mongodb.client.model.UpdateManyModel) InsertOneResult(com.mongodb.client.result.InsertOneResult) AggregateIterable(com.mongodb.client.AggregateIterable) ReturnDocument(com.mongodb.client.model.ReturnDocument) CodecRegistries(org.bson.codecs.configuration.CodecRegistries) EstimatedDocumentCountOptions(com.mongodb.client.model.EstimatedDocumentCountOptions) Objects(java.util.Objects) List(java.util.List) FindIterable(com.mongodb.client.FindIterable) Codec(org.bson.codecs.Codec) TimeSeriesOptions(com.mongodb.client.model.TimeSeriesOptions) NotNull(org.jetbrains.annotations.NotNull) ListIndexesIterable(com.mongodb.client.ListIndexesIterable) ReadPreference(com.mongodb.ReadPreference) ValueCodecProvider(org.bson.codecs.ValueCodecProvider) ReplaceOneModel(com.mongodb.client.model.ReplaceOneModel) MongoClient(com.mongodb.client.MongoClient) MongoCollection(com.mongodb.client.MongoCollection) BulkWriteOptions(com.mongodb.client.model.BulkWriteOptions) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) BsonString(org.bson.BsonString) Supplier(java.util.function.Supplier) ChangeStreamIterable(com.mongodb.client.ChangeStreamIterable) BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) TransactionOptions(com.mongodb.TransactionOptions) FindOneAndDeleteOptions(com.mongodb.client.model.FindOneAndDeleteOptions) MongoCursor(com.mongodb.client.MongoCursor) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) InsertOneModel(com.mongodb.client.model.InsertOneModel) Objects.requireNonNull(java.util.Objects.requireNonNull) BsonInt64(org.bson.BsonInt64) InsertOneOptions(com.mongodb.client.model.InsertOneOptions) TimeSeriesGranularity(com.mongodb.client.model.TimeSeriesGranularity) BsonCodecProvider(org.bson.codecs.BsonCodecProvider) ListDatabasesIterable(com.mongodb.client.ListDatabasesIterable) UpdateOneModel(com.mongodb.client.model.UpdateOneModel) InsertManyOptions(com.mongodb.client.model.InsertManyOptions) IndexOptions(com.mongodb.client.model.IndexOptions) TimeUnit(java.util.concurrent.TimeUnit) Collectors.toList(java.util.stream.Collectors.toList) ReadConcernLevel(com.mongodb.ReadConcernLevel) DistinctIterable(com.mongodb.client.DistinctIterable) BulkWriteResult(com.mongodb.bulk.BulkWriteResult) DeleteResult(com.mongodb.client.result.DeleteResult) FindOneAndReplaceOptions(com.mongodb.client.model.FindOneAndReplaceOptions) ClientSession(com.mongodb.client.ClientSession) BsonInt32(org.bson.BsonInt32) WriteConcern(com.mongodb.WriteConcern) Collections(java.util.Collections) BsonDocument(org.bson.BsonDocument) BulkWriteOptions(com.mongodb.client.model.BulkWriteOptions) BsonString(org.bson.BsonString) WriteModel(com.mongodb.client.model.WriteModel) Map(java.util.Map) BsonValue(org.bson.BsonValue)

Example 7 with BulkWriteOptions

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);
    }
}
Also used : BulkWriteOptions(com.mongodb.client.model.BulkWriteOptions) ReplaceOneModel(com.mongodb.client.model.ReplaceOneModel) ArrayList(java.util.ArrayList) UpdateOptions(com.mongodb.client.model.UpdateOptions) StopWatch(core.framework.util.StopWatch)

Aggregations

BulkWriteOptions (com.mongodb.client.model.BulkWriteOptions)7 ArrayList (java.util.ArrayList)6 BulkWriteResult (com.mongodb.bulk.BulkWriteResult)5 WriteModel (com.mongodb.client.model.WriteModel)5 BsonDocument (org.bson.BsonDocument)4 DeleteOneModel (com.mongodb.client.model.DeleteOneModel)3 ReplaceOneModel (com.mongodb.client.model.ReplaceOneModel)3 UpdateOneModel (com.mongodb.client.model.UpdateOneModel)3 UpdateOptions (com.mongodb.client.model.UpdateOptions)3 BsonValue (org.bson.BsonValue)3 MongoBulkWriteException (com.mongodb.MongoBulkWriteException)2 WriteConcern (com.mongodb.WriteConcern)2 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)2 BasicDBObject (com.mongodb.BasicDBObject)1 ReadConcern (com.mongodb.ReadConcern)1 ReadConcernLevel (com.mongodb.ReadConcernLevel)1 ReadPreference (com.mongodb.ReadPreference)1 TransactionOptions (com.mongodb.TransactionOptions)1 BulkWriteError (com.mongodb.bulk.BulkWriteError)1 BulkWriteUpsert (com.mongodb.bulk.BulkWriteUpsert)1