Search in sources :

Example 26 with ClientSession

use of com.mongodb.client.ClientSession in project mongo-java-driver by mongodb.

the class UnifiedCrudHelper method executeCreateCollection.

public OperationResult executeCreateCollection(final BsonDocument operation) {
    MongoDatabase database = entities.getDatabase(operation.getString("object").getValue());
    BsonDocument arguments = operation.getDocument("arguments");
    String collectionName = arguments.getString("collection").getValue();
    ClientSession session = getSession(arguments);
    CreateCollectionOptions options = new CreateCollectionOptions();
    for (Map.Entry<String, BsonValue> cur : arguments.entrySet()) {
        switch(cur.getKey()) {
            case "collection":
            case "session":
                break;
            case "expireAfterSeconds":
                options.expireAfter(cur.getValue().asNumber().longValue(), TimeUnit.SECONDS);
                break;
            case "timeseries":
                options.timeSeriesOptions(createTimeSeriesOptions(cur.getValue().asDocument()));
                break;
            default:
                throw new UnsupportedOperationException("Unsupported argument: " + cur.getKey());
        }
    }
    return resultOf(() -> {
        if (session == null) {
            database.createCollection(collectionName, options);
        } else {
            database.createCollection(session, collectionName, options);
        }
        return null;
    });
}
Also used : BsonDocument(org.bson.BsonDocument) ClientSession(com.mongodb.client.ClientSession) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) BsonString(org.bson.BsonString) Map(java.util.Map) MongoDatabase(com.mongodb.client.MongoDatabase) BsonValue(org.bson.BsonValue)

Example 27 with ClientSession

use of com.mongodb.client.ClientSession in project mongo-java-driver by mongodb.

the class UnifiedCrudHelper method createFindIterable.

@NotNull
private FindIterable<BsonDocument> createFindIterable(final BsonDocument operation) {
    MongoCollection<BsonDocument> collection = entities.getCollection(operation.getString("object").getValue());
    BsonDocument arguments = operation.getDocument("arguments");
    ClientSession session = getSession(arguments);
    BsonDocument filter = arguments.getDocument("filter");
    FindIterable<BsonDocument> iterable = session == null ? collection.find(filter) : collection.find(session, filter);
    for (Map.Entry<String, BsonValue> cur : arguments.entrySet()) {
        switch(cur.getKey()) {
            case "session":
            case "filter":
                break;
            case "sort":
                iterable.sort(cur.getValue().asDocument());
                break;
            case "batchSize":
                iterable.batchSize(cur.getValue().asInt32().intValue());
                break;
            case "limit":
                iterable.limit(cur.getValue().asInt32().intValue());
                break;
            case "allowDiskUse":
                iterable.allowDiskUse(cur.getValue().asBoolean().getValue());
                break;
            default:
                throw new UnsupportedOperationException("Unsupported argument: " + cur.getKey());
        }
    }
    return iterable;
}
Also used : BsonDocument(org.bson.BsonDocument) ClientSession(com.mongodb.client.ClientSession) BsonString(org.bson.BsonString) Map(java.util.Map) BsonValue(org.bson.BsonValue) NotNull(org.jetbrains.annotations.NotNull)

Example 28 with ClientSession

use of com.mongodb.client.ClientSession in project mongo-java-driver by mongodb.

the class UnifiedCrudHelper method executeAggregate.

OperationResult executeAggregate(final BsonDocument operation) {
    String entityName = operation.getString("object").getValue();
    BsonDocument arguments = operation.getDocument("arguments");
    ClientSession session = getSession(arguments);
    List<BsonDocument> pipeline = arguments.getArray("pipeline").stream().map(BsonValue::asDocument).collect(toList());
    AggregateIterable<BsonDocument> iterable;
    if (entities.hasDatabase(entityName)) {
        iterable = session == null ? entities.getDatabase(entityName).aggregate(requireNonNull(pipeline), BsonDocument.class) : entities.getDatabase(entityName).aggregate(session, requireNonNull(pipeline), BsonDocument.class);
    } else if (entities.hasCollection(entityName)) {
        iterable = session == null ? entities.getCollection(entityName).aggregate(requireNonNull(pipeline)) : entities.getCollection(entityName).aggregate(session, requireNonNull(pipeline));
    } else {
        throw new UnsupportedOperationException("Unsupported entity type with name: " + entityName);
    }
    for (Map.Entry<String, BsonValue> cur : arguments.entrySet()) {
        switch(cur.getKey()) {
            case "pipeline":
            case "session":
                break;
            case "batchSize":
                iterable.batchSize(cur.getValue().asNumber().intValue());
                break;
            case "allowDiskUse":
                iterable.allowDiskUse(cur.getValue().asBoolean().getValue());
                break;
            case "let":
                iterable.let(cur.getValue().asDocument());
                break;
            default:
                throw new UnsupportedOperationException("Unsupported argument: " + cur.getKey());
        }
    }
    String lastStageName = pipeline.isEmpty() ? null : pipeline.get(pipeline.size() - 1).getFirstKey();
    boolean useToCollection = Objects.equals(lastStageName, "$out") || Objects.equals(lastStageName, "$merge");
    return resultOf(() -> {
        if (!pipeline.isEmpty() && useToCollection) {
            iterable.toCollection();
            return null;
        } else {
            return new BsonArray(iterable.into(new ArrayList<>()));
        }
    });
}
Also used : BsonDocument(org.bson.BsonDocument) ClientSession(com.mongodb.client.ClientSession) BsonArray(org.bson.BsonArray) ArrayList(java.util.ArrayList) BsonString(org.bson.BsonString) Map(java.util.Map) BsonValue(org.bson.BsonValue)

Example 29 with ClientSession

use of com.mongodb.client.ClientSession in project mongo-java-driver by mongodb.

the class UnifiedCrudHelper method executeCountDocuments.

public OperationResult executeCountDocuments(final BsonDocument operation) {
    MongoCollection<BsonDocument> collection = entities.getCollection(operation.getString("object").getValue());
    BsonDocument arguments = operation.getDocument("arguments");
    BsonDocument filter = arguments.getDocument("filter");
    ClientSession session = getSession(arguments);
    for (Map.Entry<String, BsonValue> cur : arguments.entrySet()) {
        switch(cur.getKey()) {
            case "filter":
            case "session":
                break;
            default:
                throw new UnsupportedOperationException("Unsupported argument: " + cur.getKey());
        }
    }
    return resultOf(() -> {
        if (session == null) {
            return new BsonInt64(collection.countDocuments(filter));
        } else {
            return new BsonInt64(collection.countDocuments(session, filter));
        }
    });
}
Also used : BsonInt64(org.bson.BsonInt64) BsonDocument(org.bson.BsonDocument) ClientSession(com.mongodb.client.ClientSession) BsonString(org.bson.BsonString) Map(java.util.Map) BsonValue(org.bson.BsonValue)

Example 30 with ClientSession

use of com.mongodb.client.ClientSession in project mongo-java-driver by mongodb.

the class UnifiedCrudHelper method executeInsertOne.

OperationResult executeInsertOne(final BsonDocument operation) {
    MongoCollection<BsonDocument> collection = entities.getCollection(operation.getString("object").getValue());
    BsonDocument arguments = operation.getDocument("arguments");
    ClientSession session = getSession(arguments);
    BsonDocument document = arguments.getDocument("document").asDocument();
    InsertOneOptions options = new InsertOneOptions();
    for (Map.Entry<String, BsonValue> cur : arguments.entrySet()) {
        switch(cur.getKey()) {
            case "session":
            case "document":
                break;
            default:
                throw new UnsupportedOperationException("Unsupported argument: " + cur.getKey());
        }
    }
    return resultOf(() -> toExpected(session == null ? collection.insertOne(document, options) : collection.insertOne(session, document, options)));
}
Also used : BsonDocument(org.bson.BsonDocument) InsertOneOptions(com.mongodb.client.model.InsertOneOptions) ClientSession(com.mongodb.client.ClientSession) BsonString(org.bson.BsonString) Map(java.util.Map) BsonValue(org.bson.BsonValue)

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