getCount method
Gets the count of documents in the collection.
This method returns the total number of documents matching the specified criteria. If no criteria are provided, it returns the total count of all documents in the collection.
Parameters:
field
- Optional field name to filter byvalue
- Optional value to match for the specified fieldfilter
- Optional additional filter criteria
Returns the number of matching documents.
Example:
// Get total count
int total = await collection.getCount();
// Get count of active users
int activeUsers = await collection.getCount(
field: 'status',
value: 'active'
);
Implementation
Future<int> getCount({
String? field,
Object? value,
Map<String, Object?>? filter,
}) async {
if (field != null && value != null) {
var count = await collection.modernCount(
selector: where.eq(field, value), filter: filter);
return count.count;
}
return (await collection.modernCount(filter: filter)).count;
}