Skip to main content
Collections in Orionjs provide a type-safe interface to MongoDB collections with automatic schema validation and helpful query methods.

Creating Collections

There are two main ways to create collections in Orionjs: The recommended approach is to use the repository pattern with proper typing:

Using createCollection Directly

You can also create collections directly:

Configuration Options

The createCollection function accepts the following options:

ID Generation

Orionjs offers several options for generating document IDs:

Basic ID Generation Options

  • 'mongo' (default): MongoDB ObjectId strings (time-based)
  • 'random': Random string IDs
  • 'uuid': UUID v7 strings

Typed IDs

For TypeScript users, Orionjs provides powerful ways to create type-safe IDs using typedId:
The library will automatically detect typedId from the schema and use it to create prefixed IDs.

Indexes

Define indexes to optimize queries:

Collection Types

When using TypeScript, properly type your collections:

Getting a Collection Instance

If you need to access a collection outside a repository:
Collections provide a solid foundation for working with MongoDB in Orionjs, with built-in type safety and schema validation to make your database operations more reliable.