Skip to main content

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 data
  • posts - Blog posts or social media content
  • products - E-commerce product catalog
  • orders - 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 to
  • createdAt - Creation timestamp (auto-generated)
  • updatedAt - Last update timestamp (auto-generated)
  • data - Object containing your custom fields
Document Structure:
Important: All your custom fields are stored inside the 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.

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, not Users
  • Use plural - posts, not post
  • Use underscores for multi-word - blog_posts
  • Keep names descriptive - product_reviews, not pr
  • Avoid special characters except _ and -
Good names:
Avoid:

Document IDs

Every document has a unique id field:
Custom IDs must be unique within the collection. Attempting to create a document with an existing ID will fail.

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:

List Collections

Delete a Collection

Deleting a collection permanently removes all documents. This action cannot be undone.

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

Each document should represent a single entity. Don’t try to store your entire application state in one document.
Add indexes for fields you query often to improve performance.
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