Skip to content

文档模型

mongox 支持对结构体(Collection 的泛型参数)进行标签化,以便在插入和更新文档时自动填充字段值。这样可以减少重复的代码,提高开发效率。

mongox.Model

go mongox内置了一个Model结构体,它包含了 IDCreatedAtUpdatedAt 三个字段。

go
type Model struct {
	ID        bson.ObjectID `bson:"_id,omitempty" mongox:"autoID"`
	CreatedAt time.Time     `bson:"created_at"`
	UpdatedAt time.Time     `bson:"updated_at"`
	DeletedAt time.Time     `bson:"deleted_at,omitempty"`
}
  • 字段说明:
  • ID:文档的唯一标识,包含 mongox:"autoID" 标签,表示在插入文档时会自动设置为 ObjectID 类型的值。
  • CreatedAt:文档的创建时间,插入文档时如果该字段的值为零值,则会自动设置为当前时间。
  • UpdatedAt:文档的更新时间,插入文档时如果该字段的值为零值或更新文档时,会自动设置为当前时间。
  • DeletedAt:文档的删除时间。

你可以把它嵌入到自己定义的结构体中,如下所示:

go
type User struct {
	mongox.Model `bson:",inline"`
	Name         string `bson:"name"`
	Age          int    `bson:"age"`
}

高级用法

ID 字段填充

如果定义的结构体中包含类型为 bson.ObjectIDID 字段并且包含 mongox:"autoID" 标签,mongox会自动设置 ID 字段的值。

go
type User struct {
    ID bson.ObjectID `bson:"_id,omitempty" mongox:"autoID"`
    Name string `bson:"name"`
    Age int `bson:"age"`
}

创建和更新时间字段填充

如果所定义的结构体中包含 CreatedAtUpdatedAt 字段,mongox会自动更新这些字段的值。它们支持 time.Time 类型和 int 类型以及 int64 类型。

如果需要使用非 CreatedAtUpdatedAt 字段名,可以配置 autoCreateTimeautoUpdateTime 标签。

go
type User struct {
	CreatedAt        time.Time // 在插入文档时,如果该字段的值为零值,则会自动设置为当前时间
	UpdatedAt        int       // 在插入文档时,如果该字段的值为零值或更新文档时,会自动设置为当前时间戳秒数
	CreateSecondTime int64     `mongox:"autoCreateTime"`        // 使用秒级时间戳填充字段
	UpdateSecondTime int64     `mongox:"autoUpdateTime:second"` // 使用秒级时间戳填充字段
	CreateMilliTime  int64     `mongox:"autoCreateTime:milli"`  // 使用毫秒级时间戳填充字段
	UpdateMilliTime  int64     `mongox:"autoUpdateTime:milli"`  // 使用毫秒级时间戳填充字段
	CreateNanoTime   int64     `mongox:"autoCreateTime:nano"`   // 使用纳秒级时间戳填充字段
	UpdateNanoTime   int64     `mongox:"autoUpdateTime:nano"`   // 使用纳秒级时间戳填充字段
}

字段标签

mongox支持以下字段标签:

标签名说明
autoID在插入文档时,如果该字段的值为零值,则会自动设置为 ObjectID 类型的值
autoCreateTime在插入文档时,如果该字段的值为零值,则会自动设置为当前时间。除了 time.Time 类型,你还可以使用 secondmillinano 三种时间戳精度
autoUpdateTime在插入文档时,如果该字段的值为零值或更新文档时,会自动设置为当前时间。除了 time.Time 类型,你还可以使用 secondmillinano 三种时间戳精度