Skip to main content

Advanced Operations

Unlock the full power of CocoBase with batch operations, aggregations, transactions, and advanced data processing techniques.

Batch Operations

Handle multiple documents efficiently with batch operations. Available across all SDKs.

Batch Create

Create multiple documents in a single request:

Batch Update

Update multiple documents at once:

Batch Delete

Delete multiple documents efficiently:

Best Practices for Batch Operations

Process Large Datasets in Chunks:
Handle Errors Gracefully:

Count Documents

Efficiently retrieve the number of documents in a collection matching a filter.

Aggregations

Calculate statistics across your data efficiently.

Sum

Calculate total of a field:

Average

Calculate average value:

Min/Max

Find minimum and maximum values:

Aggregations with Filters

Calculate statistics on filtered data:

Real-World Aggregation Examples

Statistics Dashboard:
Price Analysis:

Group By

Group documents by field values and get counts. Note: groupByField is available in Flutter and Python SDKs only. It is not available in the JavaScript SDK.

Basic Grouping

Group With Filters

Grouping Patterns

Dashboard Statistics:
Activity Report:

Transactions

Handle multiple operations atomically (availability depends on your backend).

Client-Side Transaction Pattern


Performance Optimization

1. Use Indexes

Create indexes on frequently queried fields:
Guidelines:
  • Index fields used in where clauses
  • Index fields used for sorting
  • Don’t over-index (impacts write performance)
  • Avoid indexing large text fields

2. Pagination

Always paginate large datasets:
Infinite Scroll Example:

3. Caching

Implement client-side caching to reduce API calls:

4. Select Specific Fields

Only fetch needed fields to reduce bandwidth:

5. Lazy Loading

Load data on demand:

Caching Strategies

Memory Cache

Local Storage Cache (Browser/Flutter)


Indexing Best Practices

When to Index

  • Fields used frequently in where clauses
  • Fields used for sorting
  • Fields used in relationships
  • Fields with high cardinality (many unique values)

When NOT to Index

  • Large text fields
  • Fields that change frequently
  • Low cardinality fields (few unique values like boolean)
  • Fields rarely queried

Example Index Strategy


Type Conversion and Type Safety

Flutter Type-Safe Models

JavaScript Type Safety with TypeScript


Custom Data Models

Nested Objects

Polymorphic Types


Monitoring and Debugging

Query Performance Measurement

Request Logging



Deprecated Methods

[!WARNING] The followings methods called directly on the db instance are deprecated. Use the db.auth namespace instead to ensure compatibility with future SDK versions and to access the latest security features.
  • db.login() → Use db.auth.login()
  • db.register() → Use db.auth.register()
  • db.logout() → Use db.auth.logout()
  • db.isAuthenticated() → Use db.auth.isAuthenticated()
  • db.getCurrentUser() → Use db.auth.getCurrentUser()

Next Steps