Search in sources :

Example 51 with ClientSession

use of com.mongodb.client.ClientSession in project morphia by mongodb.

the class PipelineUpdate method execute.

/**
 * Executes the update
 *
 * @param options the options to apply
 * @return the results
 */
public UpdateResult execute(UpdateOptions options) {
    List<Document> updateOperations = toDocument();
    final Document queryObject = query.toDocument();
    ClientSession session = datastore.findSession(options);
    MongoCollection<T> mongoCollection = options.prepare(collection);
    if (options.isMulti()) {
        return session == null ? mongoCollection.updateMany(queryObject, updateOperations, options) : mongoCollection.updateMany(session, queryObject, updateOperations, options);
    } else {
        return session == null ? mongoCollection.updateOne(queryObject, updateOperations, options) : mongoCollection.updateOne(session, queryObject, updateOperations, options);
    }
}
Also used : ClientSession(com.mongodb.client.ClientSession) Document(org.bson.Document)

Example 52 with ClientSession

use of com.mongodb.client.ClientSession in project drill by apache.

the class MongoModify method executeOperations.

private void executeOperations(List<MongoOperation> operations) {
    ClientSession clientSession = context.client().startSession();
    String res = clientSession.withTransaction(() -> {
        operations.forEach(o -> o.execute(context.table()));
        return String.format("executed %s operations", operations.size());
    });
    logger.debug(res);
}
Also used : ClientSession(com.mongodb.client.ClientSession)

Aggregations

ClientSession (com.mongodb.client.ClientSession)52 BsonDocument (org.bson.BsonDocument)19 BsonValue (org.bson.BsonValue)17 BsonString (org.bson.BsonString)16 Map (java.util.Map)15 Document (org.bson.Document)12 Test (org.junit.jupiter.api.Test)10 BsonArray (org.bson.BsonArray)6 SessionBoundMongoTemplate (org.springframework.data.mongodb.core.MongoTemplate.SessionBoundMongoTemplate)4 TransactionOptions (com.mongodb.TransactionOptions)3 MongoClient (com.mongodb.client.MongoClient)3 MongoDatabase (com.mongodb.client.MongoDatabase)3 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)3 Test (org.junit.Test)3 Point (org.springframework.data.geo.Point)3 MongoVersion (org.springframework.data.mongodb.test.util.MongoVersion)3 DeleteOptions (com.mongodb.client.model.DeleteOptions)2 FindOneAndDeleteOptions (com.mongodb.client.model.FindOneAndDeleteOptions)2 UpdateOptions (com.mongodb.client.model.UpdateOptions)2 NonNull (com.mongodb.lang.NonNull)2