---
title: MongoDB Store
description: MongoDB store for document-oriented and horizontally-scalable deployments.
---

The `store/mongo` package implements Nexus's `store.Store` interface using the grove ORM with the MongoDB driver. It stores tenants, API keys, and usage records as documents, making it a natural fit for deployments that already run MongoDB.

## Usage

```go
import (
    "github.com/xraph/grove"
    "github.com/xraph/grove/drivers/mongodriver"
    "github.com/xraph/nexus/store/mongo"
)

db, err := grove.Open(mongodriver.Open("mongodb://localhost:27017", "nexus"))
if err != nil {
    log.Fatal(err)
}

s := mongo.New(db)
if err := s.Migrate(); err != nil {
    log.Fatal(err)
}

gw := nexus.New(
    nexus.WithDatabase(s),
)
```

## Internals

| Aspect | Detail |
|--------|--------|
| Driver | grove ORM + mongodriver |
| Migrations | Grove migrations with JSON Schema validation + indexes |
| Transactions | MongoDB sessions (replica-set required for multi-doc txns) |
| Collections | `nexus_tenants`, `nexus_api_keys`, `nexus_usage_records` |

## Grove Migrations

The store exports a `Migrations` group for use with Grove's migration orchestrator. This enables tracked, versioned migrations across all stores in your application:

```go
import mongostore "github.com/xraph/nexus/store/mongo"

// mongostore.Migrations is a migrate.Group for the nexus mongo store.
// Register it with the grove migration orchestrator for coordinated migrations.
```

## When to use

- Document-oriented workloads where MongoDB is the primary data store.
- Horizontally-scaled environments requiring sharding.
- Teams already running MongoDB in their infrastructure.
