Collections & Documents
Cocobase uses a document-based data model similar to MongoDB and Firestore. Data is organized into collections containing documents (JSON objects).What is a Collection?
A collection is a group of related documents. Think of it like a table in a relational database, but without a fixed schema. Examples:users- User profiles and account dataposts- Blog posts or social media contentproducts- E-commerce product catalogorders- Customer orders
What is a Document?
A document is a JSON object containing your data. Each document has a specific structure:id- Unique identifier (auto-generated)collection- Name of the collection this document belongs tocreatedAt- Creation timestamp (auto-generated)updatedAt- Last update timestamp (auto-generated)data- Object containing your custom fields
data property. The top-level properties (id, collection, createdAt, updatedAt) are managed by Cocobase.
Schema-less Design
Collections in Cocobase are schema-less - documents in the same collection can have different fields.- JavaScript
- Dart
- Go
- Python
Supported Data Types
Cocobase supports all JSON data types:Nested Objects
You can nest objects as deeply as needed:Arrays
Arrays can contain any data type:Collection Naming
Best practices for naming collections:- Use lowercase -
users, notUsers - Use plural -
posts, notpost - Use underscores for multi-word -
blog_posts - Keep names descriptive -
product_reviews, notpr - Avoid special characters except
_and-
Document IDs
Every document has a uniqueid field:
- Auto-generated
- Custom ID
Automatic Fields
Cocobase automatically adds these fields to every document:id - Unique Identifier
createdAt - Creation Timestamp
updatedAt - Last Update Timestamp
These fields are managed automatically. You cannot override them when creating
or updating documents.
Collection Operations
Create a Collection
Collections are created automatically when you insert the first document:- JavaScript
- Dart
- Go
- Python
List Collections
- JavaScript
- Dart
- Go
- Python
Delete a Collection
- JavaScript
- Dart
- Go
- Python
Document Size Limits
- Maximum document size: 16 MB
- Maximum nesting depth: 100 levels
- Maximum array length: 100,000 elements
For large files (images, videos, documents), use File
Storage instead of embedding in documents.
Best Practices
Keep Documents Focused
Keep Documents Focused
Each document should represent a single entity. Don’t try to store your entire application state in one document.
Index Frequently Queried Fields
Index Frequently Queried Fields
Add indexes for fields you query often to improve performance.
Use Consistent Field Names
Use Consistent Field Names
Stick to a naming convention across your application.
Type Safety (TypeScript)
Use TypeScript interfaces for type-safe operations:Next Steps
CRUD Operations
Learn to create, read, update, and delete documents
Querying Data
Master filtering and searching your collections
Relationships
Connect documents with references
Data Types
Deep dive into supported data types
