mongorm

Cursors

Use FindAll() to retrieve multiple documents. It returns a *MongORMCursor[T] that wraps the MongoDB cursor with MongORM’s type system.

Basic Usage

package main

import (
    "context"
    "fmt"
    "io"

    "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"`

    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()

    todo := &ToDo{}
    orm  := mongorm.New(todo)

    cursor, err := orm.FindAll(ctx)
    if err != nil {
        panic(err)
    }
    defer cursor.Close(ctx)

    for {
        item, err := cursor.Next(ctx)
        if err != nil {
            if err == io.EOF {
                break // no more documents
            }
            panic(err)
        }

        fmt.Printf("Document: %+v\n", item.Document())
    }
}

Filtering with FindAll()

Combine Where() with FindAll() to retrieve only matching documents:

cursor, err := orm.
    Where(ToDoFields.Text.Reg("groceries")).
    FindAll(ctx)

Load All Into Memory

Use All() on the cursor to load every result into a slice at once. Only recommended for small result sets.

cursor, err := orm.FindAll(ctx)
if err != nil {
    panic(err)
}
defer cursor.Close(ctx)

items, err := cursor.All(ctx)
if err != nil {
    panic(err)
}

for _, item := range items {
    fmt.Printf("Document: %+v\n", item.Document())
}

Cursor Methods

Method Returns Description
Next(ctx) (*MongORM[T], error) Advance and decode next document. Returns io.EOF when exhausted.
All(ctx) ([]*MongORM[T], error) Decode all remaining documents into a slice.
Close(ctx) error Close cursor and release server-side resources.

Accessing Documents

Each item returned by Next() or All() is a *MongORM[T] instance. Access the decoded struct via Document():

item, err := cursor.Next(ctx)
if err == nil {
    doc := item.Document()  // *ToDo
    fmt.Println(*doc.Text)
}

Disk-Use

FindAll() automatically enables allowDiskUse on the MongoDB query, which allows large sorts and aggregations to use temporary storage rather than fail.

Closing the Cursor

Always close the cursor when done to avoid server-side resource leaks:

defer cursor.Close(ctx)

Back to Documentation Index README