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)));
}
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)));
}
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");
}
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());
}
};
}
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);
}
Aggregations