mongorm

MongORM

MongORM is a production-ready, type-safe MongoDB ORM for Go that combines generics, fluent query building, and predictable data modeling. It helps Go teams ship faster with clean CRUD workflows, typed filters, aggregation support, and transaction-safe operations.

If you are building backend APIs, SaaS products, internal tools, or data-heavy services on MongoDB, MongORM gives you a high-signal developer experience without hiding core MongoDB power.

Developer information

Open source by Azayn Labs

Home: azayn.com LinkedIn: linkedin.com/company/azayn-labs

GitHub Profile: github.com/azayn-labs

GitHub Project: MongORM

Features

Why teams choose MongORM

Installation

go get github.com/azayn-labs/mongorm

Quick Start

package main

import (
    "context"
    "fmt"

    "github.com/azayn-labs/mongorm"
    "github.com/azayn-labs/mongorm/primitives"
    "go.mongodb.org/mongo-driver/v2/bson"
)

type ToDo struct {
    ID   *bson.ObjectID `bson:"_id,omitempty" mongorm:"primary"`
    Text *string        `bson:"text,omitempty"`

    // Connection embedded in struct tags
    connectionString *string `mongorm:"mongodb://localhost:27017,connection:url"`
    database         *string `mongorm:"mydb,connection:database"`
    collection       *string `mongorm:"todos,connection:collection"`
}

type ToDoSchema struct {
    ID   *primitives.ObjectIDField
    Text *primitives.StringField
}

var ToDoFields = mongorm.FieldsOf[ToDo, ToDoSchema]()

func main() {
    ctx := context.Background()

    // Create
    todo := &ToDo{Text: mongorm.String("Buy groceries")}
    orm  := mongorm.New(todo)
    if err := orm.Save(ctx); err != nil {
        panic(err)
    }
    fmt.Printf("Created: %s\n", todo.ID.Hex())

    // Find by ID
    found := &ToDo{}
    mongorm.New(found).Where(ToDoFields.ID.Eq(*todo.ID)).First(ctx)
    fmt.Printf("Found: %s\n", *found.Text)

    // Update
    mongorm.New(&ToDo{}).
        Where(ToDoFields.ID.Eq(*todo.ID)).
        Set(&ToDo{Text: mongorm.String("Buy organic groceries")}).
        Save(ctx)

    // Delete
    mongorm.New(&ToDo{}).Where(ToDoFields.ID.Eq(*todo.ID)).Delete(ctx)
}

Documentation

Full documentation is in the docs/ folder.

Topic Description
Getting Started Installation, model definition, schema setup
Configuration Struct tags, options struct, mixed mode
Creating Documents Inserting with Save()
Finding Documents Querying with First() / Find(), Count(), and Distinct()
Updating Documents Single and bulk updates
Deleting Documents Removing documents
Bulk Write Batch insert/update/replace/delete operations
Indexes Field-based index builders and geo index setup
Aggregation Aggregation pipelines with fluent builder and typed decoding
Cursors Iterating with FindAll()
Query Building Where(), find modifiers, pagination helpers, Set(), Unset()
Primitives Type-safe field query methods
Hooks Lifecycle hooks
Transactions Running operations in MongoDB transactions
Errors Sentinel errors and handling patterns
Timestamps Automatic CreatedAt / UpdatedAt
Utility Types Pointer helpers

API Quick Reference

Quick map of commonly used entry points and where they are documented:

HTML documentation is available at html_docs/index.html.

Keywords

Go MongoDB ORM, Golang MongoDB ORM, type-safe MongoDB query builder for Go, Go generics ORM, MongoDB CRUD library for Go, MongoDB transactions in Go, MongoDB aggregation in Go, MongoDB bulk write Go, MongoDB hooks and timestamps, lightweight Go ORM.

GitHub SEO Pack

Use these when publishing the repository to maximize discoverability in GitHub search and community feeds.

Repository Description (pick one)

Suggested GitHub Topics

go, golang, mongodb, orm, mongo-orm, golang-library, backend, query-builder, type-safe, generics, aggregation, transactions, bulk-write, developer-tools, data-access

Launch Checklist

Geo Index Defaults Example

ctx := context.Background()

err := mongorm.New(&ToDo{}).EnsureGeoDefaults(
    ctx,
    ToDoFields.Location,
    []bson.E{mongorm.Asc(ToDoFields.CreatedAt)},
)
if err != nil {
    panic(err)
}

License

See LICENSE.