Search in sources :

Example 46 with ClientSession

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

the class MorphiaQuery method iterable.

@NonNull
private <E> FindIterable<E> iterable(FindOptions findOptions, MongoCollection<E> collection) {
    final Document query = toDocument();
    if (LOG.isTraceEnabled()) {
        LOG.trace(format("Running query(%s) : %s, options: %s,", getCollectionName(), query, findOptions));
    }
    if ((findOptions.getCursorType() != null && findOptions.getCursorType() != NonTailable) && (findOptions.getSort() != null)) {
        LOG.warn("Sorting on tail is not allowed.");
    }
    ClientSession clientSession = datastore.findSession(findOptions);
    MongoCollection<E> updated = findOptions.prepare(collection);
    FindIterable<E> iterable = clientSession != null ? updated.find(clientSession, query) : updated.find(query);
    return iterable;
}
Also used : ClientSession(com.mongodb.client.ClientSession) Document(org.bson.Document) NonNull(com.mongodb.lang.NonNull)

Example 47 with ClientSession

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

the class MorphiaQuery method findAndDelete.

@Override
public T findAndDelete(FindAndDeleteOptions options) {
    MongoCollection<T> mongoCollection = options.prepare(getCollection());
    ClientSession session = datastore.findSession(options);
    return session == null ? mongoCollection.findOneAndDelete(getQueryDocument(), options) : mongoCollection.findOneAndDelete(session, getQueryDocument(), options);
}
Also used : ClientSession(com.mongodb.client.ClientSession)

Example 48 with ClientSession

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

the class Modify method execute.

/**
 * Performs the operation
 *
 * @param options the options to apply
 * @return the operation result
 */
@Nullable
public T execute(ModifyOptions options) {
    ClientSession session = getDatastore().findSession(options);
    Document update = toDocument();
    return session == null ? options.prepare(getCollection()).findOneAndUpdate(getQuery().toDocument(), update, options) : options.prepare(getCollection()).findOneAndUpdate(session, getQuery().toDocument(), update, options);
}
Also used : ClientSession(com.mongodb.client.ClientSession) Document(org.bson.Document) Nullable(com.mongodb.lang.Nullable)

Example 49 with ClientSession

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

the class LegacyQuery method iterable.

@NonNull
private <E> FindIterable<E> iterable(FindOptions options, MongoCollection<E> collection) {
    final Document query = this.toDocument();
    if (LOG.isTraceEnabled()) {
        LOG.trace(format("Running query(%s) : %s, options: %s,", getCollectionName(), query.toJson(), options));
    }
    if ((options.getCursorType() != null && options.getCursorType() != NonTailable) && (options.getSort() != null)) {
        LOG.warn("Sorting on tail is not allowed.");
    }
    ClientSession clientSession = datastore.findSession(options);
    FindIterable<E> iterable = clientSession != null ? collection.find(clientSession, query) : collection.find(query);
    return iterable;
}
Also used : ClientSession(com.mongodb.client.ClientSession) Document(org.bson.Document) NonNull(com.mongodb.lang.NonNull)

Example 50 with ClientSession

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

the class Update method execute.

/**
 * Executes the update
 *
 * @param options the options to apply
 * @return the results
 */
public UpdateResult execute(UpdateOptions options) {
    Document updateOperations = toDocument();
    final Document queryObject = getQuery().toDocument();
    ClientSession session = getDatastore().findSession(options);
    MongoCollection<T> mongoCollection = options.prepare(getCollection());
    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)

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