use of com.mongodb.client.model.FindOneAndDeleteOptions in project mongo-java-driver by mongodb.
the class FindAndDeleteAcceptanceTest method shouldRemoveOnlyTheFirstValueMatchedByFilter.
@Test
public void shouldRemoveOnlyTheFirstValueMatchedByFilter() {
// given
String secondKey = "secondKey";
Document documentToRemove = new Document(KEY, VALUE_TO_CARE_ABOUT).append(secondKey, 1);
//inserting in non-ordered fashion
collection.insertOne(new Document(KEY, VALUE_TO_CARE_ABOUT).append(secondKey, 2));
collection.insertOne(new Document(KEY, VALUE_TO_CARE_ABOUT).append(secondKey, 3));
collection.insertOne(documentToRemove);
assertThat(collection.count(), is(3L));
// when
Document filter = new Document(KEY, VALUE_TO_CARE_ABOUT);
Document documentRetrieved = collection.findOneAndDelete(filter, new FindOneAndDeleteOptions().sort(new Document(secondKey, 1)));
// then
assertThat("Document should have been deleted from the collection", collection.count(), is(2L));
assertThat("Document retrieved from removeAndGet should match the document that was inserted", documentRetrieved, equalTo(documentToRemove));
}
use of com.mongodb.client.model.FindOneAndDeleteOptions in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplate method convertToFindOneAndDeleteOptions.
private static FindOneAndDeleteOptions convertToFindOneAndDeleteOptions(Document fields, Document sort) {
FindOneAndDeleteOptions result = new FindOneAndDeleteOptions();
result = result.projection(fields).sort(sort);
return result;
}
use of com.mongodb.client.model.FindOneAndDeleteOptions in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplateUnitTests method findAndRemoveShouldUseCollationWhenPresent.
// DATAMONGO-1518
@Test
public void findAndRemoveShouldUseCollationWhenPresent() {
when(collection.findOneAndDelete(any(Bson.class), any())).thenReturn(Mono.empty());
template.findAndRemove(new BasicQuery("{}").collation(Collation.of("fr")), AutogenerateableId.class).subscribe();
ArgumentCaptor<FindOneAndDeleteOptions> options = ArgumentCaptor.forClass(FindOneAndDeleteOptions.class);
verify(collection).findOneAndDelete(Mockito.any(), options.capture());
assertThat(options.getValue().getCollation().getLocale(), is("fr"));
}
use of com.mongodb.client.model.FindOneAndDeleteOptions in project mongo-java-driver by mongodb.
the class CrudTest method getFindOneAndDeleteMongoOperation.
private MongoOperationBsonDocument getFindOneAndDeleteMongoOperation(final BsonDocument arguments) {
return new MongoOperationBsonDocument() {
@Override
public void execute() {
FindOneAndDeleteOptions options = new FindOneAndDeleteOptions();
if (arguments.containsKey("projection")) {
options.projection(arguments.getDocument("projection"));
}
if (arguments.containsKey("sort")) {
options.sort(arguments.getDocument("sort"));
}
if (arguments.containsKey("collation")) {
options.collation(getCollation(arguments.getDocument("collation")));
}
collection.findOneAndDelete(arguments.getDocument("filter"), options, getCallback());
}
};
}
Aggregations