> ## Documentation Index
> Fetch the complete documentation index at: https://www.orionjs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Introduction

> A layer on top of the MongoDB driver to make it easier to use it with Orionjs

The MongoDB package in Orionjs provides a powerful abstraction layer over the native MongoDB driver, optimized for TypeScript and seamlessly integrated with Orionjs's repository pattern.

## Overview

Working directly with the MongoDB driver can be verbose and lacks type safety. The Orionjs MongoDB package addresses these challenges by providing:

* Type-safe MongoDB operations
* Seamless integration with Orionjs schemas
* Automatic validation before database operations

## Integration with Repositories

MongoDB collections are typically used within repository classes, following Orionjs's architecture:

```typescript theme={null}
import {Repository} from '@orion-js/services'
import {createCollection, typedId, InferSchemaType} from '@orion-js/mongodb'
import {schemaWithName} from '@orion-js/schema'

const UserSchema = schemaWithName('UserSchema', {
  _id: {type: typedId('user')},
  name: {type: String},
  email: {type: String}
})

type UserType = InferSchemaType<typeof UserSchema>

@Repository()
export class UserRepository {
  users = createCollection({
    name: 'users',
    schema: UserSchema,
    indexes: [
      {
        keys: {
          email: 1,
        },
      },
    ],
  })

  async findById(id: string): Promise<UserType> {
    return await this.users.findOne({_id: id})
  }

  async create(userData: Omit<UserType, '_id'>): Promise<string> {
    return await this.users.insertOne(userData)
  }
}
```

## Key Features

### Type Safety

Collections are strongly typed based on your schema definitions:

```typescript theme={null}
// TypeScript knows that user has the shape defined in UserSchema
const user = await userRepository.findById('123')
console.log(user.name) // Type-safe access
```

### Automatic Validation

Documents are automatically validated against your schema before being inserted or updated:

```typescript theme={null}
// This will throw a validation error if the data doesn't match the schema
await userRepository.create({
  name: 'John Doe',
  email: 'john@example.com'
})
```

## Next Steps

In the following sections, we'll explore how to:

* [Create and configure collections](./collections)
* [Perform operations](./operations)
* [Data loader](./dataloader)
* [Use transactions](./transactions)
* [Configure MongoDB connections](./connections)
* [Use indexes](./indexes)
