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

The `store/mongo` package implements Shield's `store.Store` interface using the grove ORM with the MongoDB driver. It stores all Shield subsystem data as documents with automatic index creation, making it a natural fit for deployments that already run MongoDB.

## Usage

```go
import (
    "context"
    "log"

    "github.com/xraph/grove"
    "github.com/xraph/grove/drivers/mongodriver"
    "github.com/xraph/shield/store/mongo"
)

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

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

## Collections

| Collection | Subsystem | Purpose |
|------------|-----------|---------|
| `shield_instincts` | instinct | Built-in safety rules and threat patterns |
| `shield_awareness` | awareness | Context-aware detection configurations |
| `shield_boundaries` | boundary | Input/output boundary enforcement rules |
| `shield_values` | values | Value-alignment rule definitions |
| `shield_judgments` | judgment | Content evaluation and scoring rules |
| `shield_reflexes` | reflex | Automatic response and action triggers |
| `shield_profiles` | profile | Composite safety profile configurations |
| `shield_scans` | scan | Content scan results and audit log |
| `shield_policies` | policy | Organizational safety policy definitions |
| `shield_pii_tokens` | pii | Tokenized PII storage for redaction/recovery |
| `shield_compliance_reports` | compliance | Generated compliance audit reports |

## 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) |
| Concurrency | Document-level locking with horizontal scalability |

## 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/shield/store/mongo"

// mongostore.Migrations is a migrate.Group for the shield 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.
