Search in sources :

Example 16 with FindOneAndUpdateOptions

use of com.mongodb.client.model.FindOneAndUpdateOptions in project engine by Lumeer.

the class MongoDbStorage method getNextSequenceNo.

@Override
public synchronized int getNextSequenceNo(final String collectionName, final String indexAttribute, final String index) {
    final FindOneAndUpdateOptions options = new FindOneAndUpdateOptions();
    options.returnDocument(ReturnDocument.AFTER);
    final Document doc = database.getCollection(collectionName).findOneAndUpdate(eq(indexAttribute, index), inc("seq", 1), options);
    if (doc == null) {
        // the sequence did not exist
        resetSequence(collectionName, indexAttribute, index);
        return 0;
    } else {
        return doc.getInteger("seq");
    }
}
Also used : FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) Document(org.bson.Document) DataDocument(io.lumeer.engine.api.data.DataDocument) ReturnDocument(com.mongodb.client.model.ReturnDocument) BsonDocument(org.bson.BsonDocument)

Aggregations

FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)16 Document (org.bson.Document)9 Bson (org.bson.conversions.Bson)7 ReturnDocument (com.mongodb.client.model.ReturnDocument)6 Test (org.junit.jupiter.api.Test)6 BsonDocument (org.bson.BsonDocument)5 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)5 Update (org.springframework.data.mongodb.core.query.Update)5 DataDocument (io.lumeer.engine.api.data.DataDocument)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 AggregationUpdate (org.springframework.data.mongodb.core.aggregation.AggregationUpdate)2 Stopwatch (com.google.common.base.Stopwatch)1 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)1 BasicDBObject (com.mongodb.BasicDBObject)1 MongoBulkWriteException (com.mongodb.MongoBulkWriteException)1 MongoException (com.mongodb.MongoException)1 ClientSession (com.mongodb.client.ClientSession)1 UpdateResult (com.mongodb.client.result.UpdateResult)1