Colt
The mongodb ODM for Go i’ve always wanted
Colt leverages Generics to provide type-safe methods and decoding of documents. It therefor requires Go 1.18+.
Installation
go get github.com/jensteichert/colt
Quick Start
package main
import (
"fmt"
"github.com/jensteichert/colt"
"go.mongodb.org/mongo-driver/bson"
)
type Todo struct {
Id string `bson:"_id,omitempty" json:"_id,omitempty"`
Title string `bson:"title" json:"title"`
}
func main() {
db := colt.Database{}
db.Connect("mongodb://...", "myDatabaseName")
todosCollection = colt.GetCollection[Todo](&db, "todos"),
newTodo := Todo{
Title: "Hello",
}
todosCollection.Insert(newTodo)
todos, err := todosCollection.Find(bson.M{"title": "Hello"})
for _, todo := range todos {
fmt.Print(todo.Title)
}
}