Search in sources :

Example 11 with CountOptions

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

the class UpdateAcceptanceTest method shouldUpdateASingleDocumentMatchingTheSelectorWhenUsingUpdateOne.

@Test
public void shouldUpdateASingleDocumentMatchingTheSelectorWhenUsingUpdateOne() {
    // Given
    Document firstDocument = new Document("a", 1).append("x", 3);
    collection.insertOne(firstDocument);
    Document secondDocument = new Document("a", 1).append("x", 3);
    collection.insertOne(secondDocument);
    // When
    Document filter = new Document("a", 1);
    Document incrementXValueByOne = new Document("$inc", new Document("x", 1));
    collection.updateOne(filter, incrementXValueByOne);
    // Then
    assertThat(collection.count(new Document("x", 4), new CountOptions()), is(1L));
}
Also used : CountOptions(com.mongodb.client.model.CountOptions) Document(org.bson.Document) Test(org.junit.Test)

Example 12 with CountOptions

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

the class JsonPoweredCrudTestHelper method getCountDocumentsResult.

BsonDocument getCountDocumentsResult(final BsonDocument collectionOptions, final BsonDocument arguments, @Nullable final ClientSession clientSession) {
    CountOptions options = new CountOptions();
    if (arguments.containsKey("skip")) {
        options.skip(arguments.getNumber("skip").intValue());
    }
    if (arguments.containsKey("limit")) {
        options.limit(arguments.getNumber("limit").intValue());
    }
    if (arguments.containsKey("collation")) {
        options.collation(getCollation(arguments.getDocument("collation")));
    }
    BsonDocument filter = arguments.getDocument("filter", new BsonDocument());
    int count;
    if (clientSession == null) {
        count = (int) getCollection(collectionOptions).countDocuments(filter, options);
    } else {
        count = (int) getCollection(collectionOptions).countDocuments(clientSession, filter, options);
    }
    return toResult(count);
}
Also used : BsonDocument(org.bson.BsonDocument) CountOptions(com.mongodb.client.model.CountOptions)

Example 13 with CountOptions

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

the class MongoTemplateUnitTests method existsShouldUseDefaultCollationWhenPresent.

// DATAMONGO-1854
@Test
void existsShouldUseDefaultCollationWhenPresent() {
    template.exists(new BasicQuery("{}"), Sith.class);
    ArgumentCaptor<CountOptions> options = ArgumentCaptor.forClass(CountOptions.class);
    verify(collection).countDocuments(any(), options.capture());
    assertThat(options.getValue().getCollation()).isEqualTo(com.mongodb.client.model.Collation.builder().locale("de_AT").build());
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) CountOptions(com.mongodb.client.model.CountOptions) Test(org.junit.jupiter.api.Test)

Example 14 with CountOptions

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

the class ReactiveMongoTemplateUnitTests method countShouldApplyQueryHintIfPresent.

// DATAMONGO-2360
@Test
void countShouldApplyQueryHintIfPresent() {
    Document queryHint = new Document("age", 1);
    template.count(new Query().withHint(queryHint), Person.class, "star-wars").subscribe();
    ArgumentCaptor<CountOptions> options = ArgumentCaptor.forClass(CountOptions.class);
    verify(collection).countDocuments(any(), options.capture());
    assertThat(options.getValue().getHint()).isEqualTo(queryHint);
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) NearQuery(org.springframework.data.mongodb.core.query.NearQuery) Query(org.springframework.data.mongodb.core.query.Query) CountOptions(com.mongodb.client.model.CountOptions) Document(org.bson.Document) Test(org.junit.jupiter.api.Test)

Example 15 with CountOptions

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

the class ReactiveMongoTemplateUnitTests method countShouldApplyQueryHintAsIndexNameIfPresent.

// DATAMONGO-2365
@Test
void countShouldApplyQueryHintAsIndexNameIfPresent() {
    template.count(new Query().withHint("idx-1"), Person.class, "star-wars").subscribe();
    ArgumentCaptor<CountOptions> options = ArgumentCaptor.forClass(CountOptions.class);
    verify(collection).countDocuments(any(), options.capture());
    assertThat(options.getValue().getHintString()).isEqualTo("idx-1");
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) NearQuery(org.springframework.data.mongodb.core.query.NearQuery) Query(org.springframework.data.mongodb.core.query.Query) CountOptions(com.mongodb.client.model.CountOptions) Test(org.junit.jupiter.api.Test)

Aggregations

CountOptions (com.mongodb.client.model.CountOptions)17 Document (org.bson.Document)8 Test (org.junit.jupiter.api.Test)7 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)6 Test (org.junit.Test)5 NearQuery (org.springframework.data.mongodb.core.query.NearQuery)4 Query (org.springframework.data.mongodb.core.query.Query)4 BsonDocument (org.bson.BsonDocument)3 Bson (org.bson.conversions.Bson)3 UpdateOptions (com.mongodb.client.model.UpdateOptions)2 EstimatedDocumentCountOptions (com.mongodb.client.model.EstimatedDocumentCountOptions)1 ClientSession (com.mongodb.reactivestreams.client.ClientSession)1 StopWatch (core.framework.util.StopWatch)1 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 MongoDBResult (org.apache.gora.mongodb.query.MongoDBResult)1 GoraException (org.apache.gora.util.GoraException)1 ArrayMemory (php.runtime.memory.ArrayMemory)1