CountOperations¶
interface CountOperations<Document : Any> : BaseOperations
Interface grouping MongoDB operations relating to counting documents.
Inheritors¶
Properties¶
context¶
@LowLevelApi
abstract val context: BsonContext
Functions¶
count¶
Counts how many documents exist in the collection.
External resources¶
See also
CountOperations.countEstimated: Faster alternative when the result doesn't need to be exact.
abstract suspend fun count(options: CountOptions<Document>.() -> Unit = {}, predicate: FilterQuery<Document>.() -> Unit): Long
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
}
External resources¶
countEstimated¶
abstract suspend fun countEstimated(): Long
Counts all documents in the collection.
This function reads collection metadata instead of actually counting through all documents. This makes it much more performant (almost no CPU nor RAM usage), but the count may be slightly out of date.
In particular, it may become inaccurate when:
-
there are orphaned documents in a shared cluster,
-
an unclean shutdown happened.
Views do not possess the required metadata. When this function is called on a view (either a MongoDB view or a filter logical view), a regular count is executed instead.
External resources¶
See also
CountOperations.count: Perform the count for real.
exists¶
open suspend fun exists(options: CountOptions<Document>.() -> Unit = {}, predicate: FilterQuery<Document>.() -> Unit): Boolean