mongo

package module
v0.6.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 27, 2026 License: MIT Imports: 15 Imported by: 0

README

go-mongo

基于官方 mongo-driver 的工程化封装,提供:

  • MongoDB 初始化(TLS、连接池、探活 Ping)
  • Mongo 命令日志输出(控制台/回调,基于 CommandMonitor)
  • 常用模型基类(ObjectID 主键 + created_at/updated_at/deleted_at)
  • 常用工具(分页、时间过滤、物理删除/软删除)

安装

go get github.com/fireflycore/go-mongo

快速开始

package main

import (
	"context"

	"github.com/fireflycore/go-mongo"
)

func main() {
	conf := &mongo.Conf{
		Address:  "127.0.0.1:27017",
		Database: "demo",
		Logger:   true,
	}

	conf.WithLoggerConsole(true)
	conf.WithLoggerHandle(func(b []byte) {
		_ = b
	})

	db, err := mongo.New(conf)
	if err != nil {
		panic(err)
	}

	_ = db.Collection("demo").FindOne(context.Background(), map[string]any{})
}

配置说明

初始化配置为 mongo.Conf

常用字段:

  • Address:MongoDB 地址,通常为 host:port(内部会拼接为 mongodb://{Address})
  • Database/Username/Password:连接信息(Username 不为空时启用认证)
  • Tls:TLS 配置(见下文)
  • MaxOpenConnects:连接池最大连接数(映射到 maxPoolSize)
  • ConnMaxLifeTime:连接最大空闲时间(单位:秒,<=0 表示不设置)
  • Logger:启用 Mongo 命令日志(配合 WithLoggerConsole / WithLoggerHandle)

说明:

  • MaxIdleConnects 目前未设置到 mongo-driver 的 options 中,属于预留字段

TLS

Conf.Tls 同时配置了 CaCert / ClientCert / ClientCertKey 三个文件路径时启用 TLS,否则视为不启用:

conf := &mongo.Conf{
	Address:  "127.0.0.1:27017",
	Database: "demo",
	Tls: &mongo.TLS{
		CaCert:        "/path/to/ca.pem",
		ClientCert:    "/path/to/client.pem",
		ClientCertKey: "/path/to/client.key",
	},
}

Mongo 命令日志回调

开启 Logger 后,你可以把 Mongo 命令日志输出到控制台,或写入自定义回调:

conf := &mongo.Conf{
	Address:  "127.0.0.1:27017",
	Database: "demo",
	Logger:   true,
}

conf.WithLoggerConsole(true)
conf.WithLoggerHandle(func(b []byte) {
	_ = b
})

回调收到的是一段 JSON bytes。若你的 Mongo 操作使用了 collection.Find(ctx, ...) 等并且 ctx 是 gRPC 的 incoming context,则会尝试从 metadata 里读取链路字段:

  • trace-id
  • user-id
  • app-id

模型基类

go-mongo 提供 mongo.Table 可直接嵌入到你的实体中:

type TestEntity struct {
	mongo.Table
}

注意:BeforeInsert/BeforeUpdate 需要你在写入前手动调用(本库不会自动注册 driver hook)。

常用工具

分页

import (
	"go.mongodb.org/mongo-driver/mongo/options"
)

opt := options.Find()
mongo.WithPagination(opt, 1, 20)

分页规则:

  • page 从 1 开始(0 会被修正为 1)
  • size 范围为 [5, 100](0 会被修正为 5,大于 100 修正为 100)

时间过滤

WithTimerFilter 会向 filter 追加 created_at 的时间范围条件,start/end 需要是 Go 的 time.DateTime 格式(如 2006-01-02 15:04:05):

import (
	"go.mongodb.org/mongo-driver/bson"
)

filter := bson.D{}
mongo.WithTimerFilter("2026-01-01 00:00:00", "2026-01-02 00:00:00", &filter)

删除/软删除

// 物理删除
_, err := mongo.DeleteById(ctx, collection, id)

// 软删除:写入 updated_at/deleted_at(UTC)
_, err := mongo.SoftDeleteById(ctx, collection, id)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete

func Delete(ctx context.Context, collection *mongo.Collection, id string) (*mongo.DeleteResult, error)

DeleteById 按id删除单条文档,并返回 driver 的 DeleteResult。

func DeleteManyByIds

func DeleteManyByIds(ctx context.Context, collection *mongo.Collection, ids []string) (*mongo.DeleteResult, error)

DeleteManyByIds 按id列表批量删除文档,并返回 driver 的 DeleteResult。

func New

func New(c *Conf) (*mongo.Database, error)

New 根据配置创建 MongoDB 连接并返回数据库句柄。

func NewUUIDv7

func NewUUIDv7() string

NewUUIDv7 生成一个 UUIDv7 字符串

func SoftDeleteById

func SoftDeleteById(ctx context.Context, collection *mongo.Collection, id string) (*mongo.UpdateResult, error)

SoftDeleteById 软删除单条文档:写入 updated_at 与 deleted_at,并返回 UpdateResult。

func SoftDeleteManyByIds

func SoftDeleteManyByIds(ctx context.Context, collection *mongo.Collection, ids []string) (*mongo.UpdateResult, error)

SoftDeleteManyByIds 软删除多条文档:批量写入 updated_at 与 deleted_at,并返回 UpdateResult。

Types

type Conf

type Conf struct {
	Address  string `json:"address"`
	Database string `json:"database"`
	Username string `json:"username"`
	Password string `json:"password"`

	// Tls 为 TLS 配置,非空且字段齐全时启用双向 TLS。
	Tls *tlsx.TLS `json:"tls"`

	// MaxOpenConnects 用于控制连接池最大连接数(映射到 maxPoolSize)。
	MaxOpenConnects int `json:"max_open_connects"`
	// ConnMaxLifeTime 为连接最大空闲时间(秒),用于回收长时间空闲连接。
	ConnMaxLifeTime int `json:"conn_max_life_time"`

	// Logger 控制是否启用 Mongo 命令监控日志
	Logger bool `json:"logger"`
	// contains filtered or unexported fields
}

Conf 定义 MongoDB 连接初始化所需的配置项。

func (*Conf) WithLoggerConsole

func (c *Conf) WithLoggerConsole(state bool)

WithLoggerConsole 设置是否将日志输出到控制台。

func (*Conf) WithLoggerHandle

func (c *Conf) WithLoggerHandle(handle func(b []byte))

WithLoggerHandle 设置结构化日志回调(用于接入自有日志系统)。

type Table

type Table struct {
	Id        string     `json:"id" bson:"_id"`
	CreatedAt time.Time  `json:"created_at" bson:"created_at"`
	UpdatedAt time.Time  `json:"updated_at" bson:"updated_at"`
	DeletedAt *time.Time `json:"deleted_at" bson:"deleted_at,omitempty"`
}

Table 为通用表结构字段集合(UUID_V7 + 时间戳 + 软删除)。

func (*Table) BeforeInsert

func (t *Table) BeforeInsert()

BeforeInsert 为新记录初始化主键与时间字段。

func (*Table) BeforeUpdate

func (t *Table) BeforeUpdate()

BeforeUpdate 在更新前刷新 UpdatedAt 字段。

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL