Count Documents
Query Operations
Count Documents
Count documents matching a filter query with RBAC enforcement
POST
Count Documents
Overview
The count endpoint returns the number of documents in a collection that match a given filter query. The count respects RBAC permissions and only includes documents the authenticated user has permission to read.Request
The name of the collection to count documents in
MongoDB-style filter query to match documents. If omitted, counts all documents the user has permission to read.
Count All Documents Example
Count with Filter Example
Complex Filter Example
Response
The number of documents matching the filter criteria that the user has permission to read
Response Example
Filter Query Syntax
Thefilter parameter supports MongoDB query operators:
Comparison Operators
$eq: Equal to$ne: Not equal to$gt: Greater than$gte: Greater than or equal to$lt: Less than$lte: Less than or equal to
Array Operators
$in: Value is in array$nin: Value is not in array
Logical Operators
$and: All conditions must match$or: At least one condition must match$not: Inverts the condition$exists: Field exists/doesn’t exist
Pattern Matching
RBAC Enforcement
Fromhandlers_query.go:470-477:
The count operation automatically combines the provided filter with RBAC query filters. This ensures users only count documents they have permission to read.
- A manager might only count documents in their department
- A user might only count their own documents
- An admin might count all documents
Use Cases
Pre-validation Before Batch Operations
Analytics and Reporting
Validation Before Updates
Performance Considerations
Count operations use MongoDB’s native count functionality, which is optimized for indexed fields. Ensure your filter uses indexed fields for best performance on large collections.
Optimization Tips
- Use indexed fields in your filter criteria
- Avoid complex regex patterns on non-indexed fields
- Consider collection size: Counts on very large collections may take time
- Cache results if the data doesn’t change frequently
Error Responses
Error Example
Audit Logging
All count operations are logged to the audit trail with:- User ID and roles
- Collection name
- Filter criteria
- Success/failure status
- Timestamp
handlers_query.go:492-493
Code Reference
Implementation:pkg/api/handlers_query.go:422-502
Related Endpoints
- List Documents - Query and retrieve documents
- Aggregate - Complex aggregations with grouping
- Batch Delete - Delete multiple documents