Use Delete() to remove a single document that matches the current filters.
Filter to the target document using Where(), then call Delete():
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"`
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()
targetID := bson.NewObjectID() // use a real existing ID
todo := &ToDo{}
orm := mongorm.New(todo)
orm.Where(ToDoFields.ID.Eq(targetID))
if err := orm.Delete(ctx); err != nil {
panic(err)
}
fmt.Println("Document deleted")
}
If you already have a document with a primary key populated (e.g., after a First() call), you can delete it without calling Where():
// Load the document first
todo := &ToDo{}
orm := mongorm.New(todo)
orm.Where(ToDoFields.Text.Eq("Buy groceries"))
if err := orm.First(ctx); err != nil {
panic(err)
}
// Now delete it — MongORM uses todo.ID as the filter automatically
if err := orm.Delete(ctx); err != nil {
panic(err)
}
Before and after a delete, the BeforeDeleteHook and AfterDeleteHook are invoked if implemented on your model. See Hooks.
func (t *ToDo) BeforeDelete(m *mongorm.MongORM[ToDo], filter *bson.M) error {
fmt.Printf("Deleting document with filter: %+v\n", *filter)
return nil
}
func (t *ToDo) AfterDelete(m *mongorm.MongORM[ToDo]) error {
fmt.Println("Document deleted")
return nil
}
Delete() only removes one document (the first match).Use DeleteMulti() to remove all documents matching the current filters.
todo := &ToDo{}
orm := mongorm.New(todo)
result, err := orm.
Where(ToDoFields.Text.Reg("^temp-")).
DeleteMulti(ctx)
if err != nil {
panic(err)
}
fmt.Printf("Deleted: %d\n", result.DeletedCount)
| Back to Documentation Index | README |