Search in sources :

Example 11 with FindOneAndUpdateOptions

use of com.mongodb.client.model.FindOneAndUpdateOptions in project spring-data-mongodb by spring-projects.

the class MongoTemplateUnitTests method findAndModifyShouldApplyArrayFilters.

// DATAMONGO-2215
@Test
void findAndModifyShouldApplyArrayFilters() {
    template.findAndModify(new BasicQuery("{}"), new Update().set("grades.$[element]", 100).filterArray(Criteria.where("element").gte(100)), EntityWithListOfSimple.class);
    ArgumentCaptor<FindOneAndUpdateOptions> options = ArgumentCaptor.forClass(FindOneAndUpdateOptions.class);
    verify(collection).findOneAndUpdate(any(), any(Bson.class), options.capture());
    Assertions.assertThat((List<Bson>) options.getValue().getArrayFilters()).contains(new org.bson.Document("element", new Document("$gte", 100)));
}
Also used : Document(org.bson.Document) BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) ArrayList(java.util.ArrayList) List(java.util.List) Update(org.springframework.data.mongodb.core.query.Update) Document(org.bson.Document) Bson(org.bson.conversions.Bson) Test(org.junit.jupiter.api.Test)

Example 12 with FindOneAndUpdateOptions

use of com.mongodb.client.model.FindOneAndUpdateOptions in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplateUnitTests method findAndModifyShouldApplyArrayFilters.

// DATAMONGO-2215
@Test
void findAndModifyShouldApplyArrayFilters() {
    template.findAndModify(new BasicQuery("{}"), new Update().set("grades.$[element]", 100).filterArray(Criteria.where("element").gte(100)), EntityWithListOfSimple.class).subscribe();
    ArgumentCaptor<FindOneAndUpdateOptions> options = ArgumentCaptor.forClass(FindOneAndUpdateOptions.class);
    verify(collection).findOneAndUpdate(any(), any(Bson.class), options.capture());
    Assertions.assertThat((List<Bson>) options.getValue().getArrayFilters()).contains(new org.bson.Document("element", new Document("$gte", 100)));
}
Also used : Document(org.bson.Document) BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) List(java.util.List) ArrayList(java.util.ArrayList) Update(org.springframework.data.mongodb.core.query.Update) AggregationUpdate(org.springframework.data.mongodb.core.aggregation.AggregationUpdate) Document(org.bson.Document) Bson(org.bson.conversions.Bson) Test(org.junit.jupiter.api.Test)

Example 13 with FindOneAndUpdateOptions

use of com.mongodb.client.model.FindOneAndUpdateOptions in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplateUnitTests method findAndModfiyShoudUseCollationWhenPresent.

// DATAMONGO-1518
@Test
void findAndModfiyShoudUseCollationWhenPresent() {
    when(collection.findOneAndUpdate(any(Bson.class), any(Bson.class), any())).thenReturn(Mono.empty());
    template.findAndModify(new BasicQuery("{}").collation(Collation.of("fr")), new Update(), AutogenerateableId.class).subscribe();
    ArgumentCaptor<FindOneAndUpdateOptions> options = ArgumentCaptor.forClass(FindOneAndUpdateOptions.class);
    verify(collection).findOneAndUpdate(any(), any(Bson.class), options.capture());
    assertThat(options.getValue().getCollation().getLocale()).isEqualTo("fr");
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) AutogenerateableId(org.springframework.data.mongodb.core.MongoTemplateUnitTests.AutogenerateableId) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) Update(org.springframework.data.mongodb.core.query.Update) AggregationUpdate(org.springframework.data.mongodb.core.aggregation.AggregationUpdate) Bson(org.bson.conversions.Bson) Test(org.junit.jupiter.api.Test)

Example 14 with FindOneAndUpdateOptions

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

the class CrudTest method getFindOneAndUpdateMongoOperation.

private MongoOperationBsonDocument getFindOneAndUpdateMongoOperation(final BsonDocument arguments) {
    return new MongoOperationBsonDocument() {

        @Override
        public void execute() {
            FindOneAndUpdateOptions options = new FindOneAndUpdateOptions();
            if (arguments.containsKey("projection")) {
                options.projection(arguments.getDocument("projection"));
            }
            if (arguments.containsKey("sort")) {
                options.sort(arguments.getDocument("sort"));
            }
            if (arguments.containsKey("upsert")) {
                options.upsert(arguments.getBoolean("upsert").getValue());
            }
            if (arguments.containsKey("returnDocument")) {
                options.returnDocument(arguments.getString("returnDocument").getValue().equals("After") ? ReturnDocument.AFTER : ReturnDocument.BEFORE);
            }
            if (arguments.containsKey("collation")) {
                options.collation(getCollation(arguments.getDocument("collation")));
            }
            collection.findOneAndUpdate(arguments.getDocument("filter"), arguments.getDocument("update"), options, getCallback());
        }
    };
}
Also used : FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions)

Example 15 with FindOneAndUpdateOptions

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

the class JsonPoweredCrudTestHelper method getFindOneAndUpdateResult.

BsonDocument getFindOneAndUpdateResult(final BsonDocument collectionOptions, final BsonDocument arguments, @Nullable final ClientSession clientSession) {
    FindOneAndUpdateOptions options = new FindOneAndUpdateOptions();
    if (arguments.containsKey("projection")) {
        options.projection(arguments.getDocument("projection"));
    }
    if (arguments.containsKey("sort")) {
        options.sort(arguments.getDocument("sort"));
    }
    if (arguments.containsKey("upsert")) {
        options.upsert(arguments.getBoolean("upsert").getValue());
    }
    if (arguments.containsKey("returnDocument")) {
        options.returnDocument(arguments.getString("returnDocument").getValue().equals("After") ? ReturnDocument.AFTER : ReturnDocument.BEFORE);
    }
    if (arguments.containsKey("collation")) {
        options.collation(getCollation(arguments.getDocument("collation")));
    }
    if (arguments.containsKey("arrayFilters")) {
        options.arrayFilters((getListOfDocuments(arguments.getArray("arrayFilters"))));
    }
    if (arguments.containsKey("hint")) {
        if (arguments.isDocument("hint")) {
            options.hint(arguments.getDocument("hint"));
        } else {
            options.hintString(arguments.getString("hint").getValue());
        }
    }
    BsonDocument result;
    if (clientSession == null) {
        if (arguments.isDocument("update")) {
            result = getCollection(collectionOptions).findOneAndUpdate(arguments.getDocument("filter"), arguments.getDocument("update"), options);
        } else {
            // update is a pipeline
            result = getCollection(collectionOptions).findOneAndUpdate(arguments.getDocument("filter"), getListOfDocuments(arguments.getArray("update")), options);
        }
    } else {
        if (arguments.isDocument("update")) {
            result = getCollection(collectionOptions).findOneAndUpdate(clientSession, arguments.getDocument("filter"), arguments.getDocument("update"), options);
        } else {
            // update is a pipeline
            result = getCollection(collectionOptions).findOneAndUpdate(clientSession, arguments.getDocument("filter"), getListOfDocuments(arguments.getArray("update")), options);
        }
    }
    return toResult(result);
}
Also used : BsonDocument(org.bson.BsonDocument) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions)

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