Search in sources :

Example 1 with FindOneAndDeleteOptions

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));
}
Also used : Document(org.bson.Document) FindOneAndDeleteOptions(com.mongodb.client.model.FindOneAndDeleteOptions) Test(org.junit.Test)

Example 2 with FindOneAndDeleteOptions

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;
}
Also used : FindOneAndDeleteOptions(com.mongodb.client.model.FindOneAndDeleteOptions)

Example 3 with FindOneAndDeleteOptions

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"));
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) AutogenerateableId(org.springframework.data.mongodb.core.MongoTemplateUnitTests.AutogenerateableId) FindOneAndDeleteOptions(com.mongodb.client.model.FindOneAndDeleteOptions) Bson(org.bson.conversions.Bson) Test(org.junit.Test)

Example 4 with FindOneAndDeleteOptions

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());
        }
    };
}
Also used : FindOneAndDeleteOptions(com.mongodb.client.model.FindOneAndDeleteOptions)

Aggregations

FindOneAndDeleteOptions (com.mongodb.client.model.FindOneAndDeleteOptions)4 Test (org.junit.Test)2 Document (org.bson.Document)1 Bson (org.bson.conversions.Bson)1 AutogenerateableId (org.springframework.data.mongodb.core.MongoTemplateUnitTests.AutogenerateableId)1 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)1