count

abstract suspend fun count(): Long(source)

Counts how many documents exist in the collection.

Implementation

This method, just like in mongosh, in syntax sugar for an aggregation pipeline using the countTo stage.

External resources

See also

Faster alternative when the result doesn't need to be exact.


abstract suspend fun count(options: CountOptions<Document>.() -> Unit = {}, predicate: FilterQuery<Document>.() -> Unit): Long(source)

Counts how many documents match predicate in the collection.

Example

class User(
val name: String,
val age: Int,
)

collection.count {
User::name eq "foo"
User::age eq 10
}

Implementation

This method, just like in mongosh, in syntax sugar for an aggregation pipeline using the match and the countTo stages.

External resources