Search in sources :

Example 1 with EstimatedDocumentCountOptions

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

the class UnifiedCrudHelper method executeEstimatedDocumentCount.

public OperationResult executeEstimatedDocumentCount(final BsonDocument operation) {
    MongoCollection<BsonDocument> collection = entities.getCollection(operation.getString("object").getValue());
    BsonDocument arguments = operation.getDocument("arguments", new BsonDocument());
    EstimatedDocumentCountOptions options = new EstimatedDocumentCountOptions();
    for (Map.Entry<String, BsonValue> cur : arguments.entrySet()) {
        // noinspection SwitchStatementWithTooFewBranches
        switch(cur.getKey()) {
            case "maxTimeMS":
                options.maxTime(cur.getValue().asNumber().intValue(), TimeUnit.MILLISECONDS);
                break;
            default:
                throw new UnsupportedOperationException("Unsupported argument: " + cur.getKey());
        }
    }
    return resultOf(() -> new BsonInt64(collection.estimatedDocumentCount(options)));
}
Also used : BsonInt64(org.bson.BsonInt64) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) EstimatedDocumentCountOptions(com.mongodb.client.model.EstimatedDocumentCountOptions) Map(java.util.Map) BsonValue(org.bson.BsonValue)

Example 2 with EstimatedDocumentCountOptions

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

the class MongoCollectionImplTest method testEstimatedDocumentCount.

@Test
public void testEstimatedDocumentCount() {
    EstimatedDocumentCountOptions options = new EstimatedDocumentCountOptions().maxTime(1, TimeUnit.MILLISECONDS);
    assertAll("estimatedDocumentCount", () -> assertAll("check validation", () -> assertThrows(IllegalArgumentException.class, () -> collection.estimatedDocumentCount(null))), () -> {
        Publisher<Long> expected = mongoOperationPublisher.estimatedDocumentCount(new EstimatedDocumentCountOptions());
        assertPublisherIsTheSameAs(expected, collection.estimatedDocumentCount(), "Default");
    }, () -> {
        Publisher<Long> expected = mongoOperationPublisher.estimatedDocumentCount(options);
        assertPublisherIsTheSameAs(expected, collection.estimatedDocumentCount(options), "With options");
    });
}
Also used : EstimatedDocumentCountOptions(com.mongodb.client.model.EstimatedDocumentCountOptions) Test(org.junit.jupiter.api.Test)

Aggregations

EstimatedDocumentCountOptions (com.mongodb.client.model.EstimatedDocumentCountOptions)2 Map (java.util.Map)1 BsonDocument (org.bson.BsonDocument)1 BsonInt64 (org.bson.BsonInt64)1 BsonString (org.bson.BsonString)1 BsonValue (org.bson.BsonValue)1 Test (org.junit.jupiter.api.Test)1