Documentation
¶
Index ¶
- func FindTyped[T any](c *Client, ctx context.Context, tableName string, filter interface{}) ([]T, error)
- func SetGlobal(instance *Client)
- type Client
- func (c *Client) Aggregate(ctx context.Context, tableName string, groupStage mongo.Pipeline) ([]bson.M, error)
- func (c *Client) Client() *mongo.Client
- func (c *Client) Collection(name string) *mongo.Collection
- func (c *Client) Count(ctx context.Context, tableName string, filter interface{}) (int64, error)
- func (c *Client) DeleteOne(ctx context.Context, tableName string, filter interface{}) error
- func (c *Client) Disconnect(ctx context.Context) error
- func (c *Client) Find(ctx context.Context, tableName string, filters ...interface{}) ([]bson.M, error)
- func (c *Client) FindOne(ctx context.Context, tableName string, filter interface{}) *mongo.SingleResult
- func (c *Client) InsertMany(ctx context.Context, tableName string, documents []interface{}) (*mongo.InsertManyResult, error)
- func (c *Client) InsertOne(ctx context.Context, tableName string, document interface{}) (interface{}, error)
- func (c *Client) Ping(ctx context.Context) error
- func (c *Client) UpdateMany(ctx context.Context, tableName string, filter, update interface{}) (*mongo.UpdateResult, error)
- func (c *Client) UpdateOne(ctx context.Context, tableName string, filter, update interface{}) (*mongo.UpdateResult, error)
- type MongoDBConfig
- type MongoTemplate
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client 封装 MongoDB 客户端与默认数据库配置。
func GetClient ¶
func GetClient(dbCig *MongoDBConfig, ctx context.Context) (*Client, error)
GetClient 创建 MongoDB 客户端;配置或 Context 为空时返回明确错误。 调用方应使用带超时的 Context,并在使用完成后调用 Disconnect。
func (*Client) Aggregate ¶
func (c *Client) Aggregate(ctx context.Context, tableName string, groupStage mongo.Pipeline) ([]bson.M, error)
Aggregate 执行聚合管道并返回全部结果,游标统一关闭并处理关闭错误。
func (*Client) Collection ¶ added in v1.22.0
func (c *Client) Collection(name string) *mongo.Collection
Collection 返回原生集合句柄。
func (*Client) Disconnect ¶
Disconnect 关闭 MongoDB 客户端连接;重复调用安全。
func (*Client) Find ¶
func (c *Client) Find(ctx context.Context, tableName string, filters ...interface{}) ([]bson.M, error)
Find 按过滤条件查询文档集合;游标统一关闭并处理解码与遍历错误。 新代码建议使用类型安全的 FindTyped。
func (*Client) FindOne ¶
func (c *Client) FindOne(ctx context.Context, tableName string, filter interface{}) *mongo.SingleResult
FindOne 查询单条文档。
func (*Client) InsertMany ¶ added in v1.22.0
func (c *Client) InsertMany(ctx context.Context, tableName string, documents []interface{}) (*mongo.InsertManyResult, error)
InsertMany 批量插入。
func (*Client) InsertOne ¶
func (c *Client) InsertOne(ctx context.Context, tableName string, document interface{}) (interface{}, error)
InsertOne 插入单条文档并返回插入 ID。
func (*Client) UpdateMany ¶ added in v1.22.0
func (c *Client) UpdateMany(ctx context.Context, tableName string, filter, update interface{}) (*mongo.UpdateResult, error)
UpdateMany 批量更新。
type MongoDBConfig ¶
type MongoDBConfig struct {
Timeout time.Duration `mapstructure:"timeout" json:"timeout" yaml:"timeout"` // 超时秒数
DB string `mapstructure:"db" json:"db" yaml:"db"`
Addr string `mapstructure:"addr" json:"addr" yaml:"addr"` // host:port
User string `mapstructure:"user" json:"user" yaml:"user"` // 可选用户名
Password string `mapstructure:"password" json:"password" yaml:"password"`
}
MongoDBConfig 定义 MongoDB 连接配置。 Password 为敏感字段,禁止写入日志或错误信息。
func (*MongoDBConfig) GetApplyURI ¶
func (md *MongoDBConfig) GetApplyURI() string
GetApplyURI 构造 MongoDB 连接串。 提供用户名时使用 URL 编码的凭据;返回的 URI 禁止写入日志。
type MongoTemplate ¶ added in v1.22.0
type MongoTemplate interface {
// Client 返回底层原生 MongoDB 客户端。
Client() *mongo.Client
// Collection 返回原生集合句柄(手动操作入口)。
Collection(name string) *mongo.Collection
// InsertMany 批量插入文档;返回插入结果(含 IDs)。
InsertMany(ctx context.Context, tableName string, documents []interface{}) (*mongo.InsertManyResult, error)
// Count 统计匹配 filter 的文档数。
Count(ctx context.Context, tableName string, filter interface{}) (int64, error)
// UpdateMany 批量更新匹配 filter 的文档;返回更新结果。
UpdateMany(ctx context.Context, tableName string, filter, update interface{}) (*mongo.UpdateResult, error)
}
MongoTemplate 高频操作层(对标 Spring Data MongoDB 的 MongoTemplate): 在 Client 现有封装(Find/InsertOne/UpdateOne/DeleteOne/Aggregate)之上, 补充批量/计数操作,并暴露原生 *mongo.Client / *mongo.Collection 供开发者手动调用原生 API。
用法:
client, _ := mongodb.GetClient(config, ctx)
client.InsertMany(ctx, "orders", []interface{}{...})
count, _ := client.Count(ctx, "orders", bson.M{"status": "paid"})
// 需要原生 API 时:
raw := client.Client() // *mongo.Client
raw.Database("db").RunCommand(ctx, ...)