mongodb

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

README

framework-mongodb

Go framework package for mongodb.

Installation

go get github.com/go-anyway/framework-mongodb@v1.0.0

Usage

See documentation for usage examples.

License

Apache License 2.0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Enabled        bool               `yaml:"enabled" env:"MONGODB_ENABLED" default:"true"`
	Host           string             `yaml:"host" env:"MONGODB_HOST" default:"localhost"`
	Port           int                `yaml:"port" env:"MONGODB_PORT" default:"27017"`
	Database       string             `yaml:"database" env:"MONGODB_DATABASE" required:"true"`
	Username       string             `yaml:"username" env:"MONGODB_USERNAME"`
	Password       string             `yaml:"password" env:"MONGODB_PASSWORD"`
	AuthSource     string             `yaml:"auth_source" env:"MONGODB_AUTH_SOURCE" default:"admin"`
	MaxPoolSize    int                `yaml:"max_pool_size" env:"MONGODB_MAX_POOL_SIZE" default:"100"`
	MinPoolSize    int                `yaml:"min_pool_size" env:"MONGODB_MIN_POOL_SIZE" default:"10"`
	ConnectTimeout pkgConfig.Duration `yaml:"connect_timeout" env:"MONGODB_CONNECT_TIMEOUT" default:"10s"`
	SocketTimeout  pkgConfig.Duration `yaml:"socket_timeout" env:"MONGODB_SOCKET_TIMEOUT" default:"30s"`
	EnableTrace    bool               `yaml:"enable_trace" env:"MONGODB_ENABLE_TRACE" default:"true"`
}

Config MongoDB 配置结构体(用于从配置文件创建)

func (*Config) ConnectTimeoutDuration

func (c *Config) ConnectTimeoutDuration() time.Duration

ConnectTimeoutDuration 返回 time.Duration 类型的 ConnectTimeout

func (*Config) SocketTimeoutDuration

func (c *Config) SocketTimeoutDuration() time.Duration

SocketTimeoutDuration 返回 time.Duration 类型的 SocketTimeout

func (*Config) ToOptions

func (c *Config) ToOptions() (*Options, error)

ToOptions 转换为 Options

func (*Config) Validate

func (c *Config) Validate() error

Validate 验证 MongoDB 配置

type MongoDBClient

type MongoDBClient struct {
	Client   *mongo.Client
	Database *mongo.Database
	// contains filtered or unexported fields
}

MongoDBClient MongoDB 客户端封装

func NewMongoDB

func NewMongoDB(opts *Options) (*MongoDBClient, error)

NewMongoDB 根据给定的选项创建一个新的 MongoDB 客户端实例

func (*MongoDBClient) Close

func (c *MongoDBClient) Close(ctx context.Context) error

Close 关闭 MongoDB 连接

func (*MongoDBClient) Collection

func (c *MongoDBClient) Collection(name string) *mongo.Collection

Collection 获取集合

func (*MongoDBClient) Ping

func (c *MongoDBClient) Ping(ctx context.Context) error

Ping 测试连接

type Options

type Options struct {
	Host           string
	Port           int
	Database       string
	Username       string
	Password       string
	AuthSource     string
	MaxPoolSize    uint64
	MinPoolSize    uint64
	ConnectTimeout time.Duration
	SocketTimeout  time.Duration
	EnableTrace    bool // 是否启用操作追踪,用于记录 MongoDB 操作执行时间
}

Options 结构体定义了 MongoDB 连接器的配置选项(内部使用)

Jump to

Keyboard shortcuts

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