mongodb

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package mongodb

Package mongodb

Package mongodb

Index

Constants

This section is empty.

Variables

View Source
var (
	PercentSignUTF8, _      = utf8.DecodeRuneInString("%")
	UnderscoreUTF8, _       = utf8.DecodeRuneInString("_")
	EscapeCharUTF8, _       = utf8.DecodeRuneInString(`\`)
	LeftSquareCharUTF8, _   = utf8.DecodeRuneInString(`[`)
	RightSquareCharUTF8, _  = utf8.DecodeRuneInString(`]`)
	LeftBracketCharUTF8, _  = utf8.DecodeRuneInString(`(`)
	RightBracketCharUTF8, _ = utf8.DecodeRuneInString(`)`)
	AsteriskCharUTF8, _     = utf8.DecodeRuneInString(`*`)
	DotCharUTF8, _          = utf8.DecodeRuneInString(`.`)
	DollarCharUTF8, _       = utf8.DecodeRuneInString(`$`)
	TipCharUTF8, _          = utf8.DecodeRuneInString(`^`)
	QMCharUTF8, _           = utf8.DecodeRuneInString(`?`)
)

一些转义符

Functions

func EscapeLikeString

func EscapeLikeString(s string) string

EscapeLikeString replace掉正则查询的字符

func IsNoFoundError

func IsNoFoundError(err error) bool

IsNoFoundError 判断不存在

func WrapErr

func WrapErr(err error) error

WrapErr 包装mongodb的错误

Types

type Client

type Client struct {
	*qmgo.Client

	Config *Config
	DB     *qmgo.Database
	Coll   *qmgo.Collection
}

Client 客户端

func NewClient

func NewClient(fn ...ClientOptionFunc) (c *Client, err error)

NewClient is 初始化mongodb客户端

func (*Client) BulkExecute

func (c *Client) BulkExecute(ctx context.Context, bulk *qmgo.Bulk) (err error)

BulkExecute 批量执行

func (*Client) Collection

func (c *Client) Collection(name string) *Client

Collection 设置集合

func (*Client) DeleteAll

func (c *Client) DeleteAll(ctx context.Context, data bson.M) error

DeleteAll 删除所有符合条件的数据

func (*Client) FindAll

func (c *Client) FindAll(ctx context.Context, filters bson.M, queryResult interface{}) error

FindAll 查询所有

func (*Client) FindOne

func (c *Client) FindOne(ctx context.Context, filters bson.M, queryResult interface{}) error

FindOne 查询单个

func (*Client) GetBulkContainer

func (c *Client) GetBulkContainer(collection string) (bulk *qmgo.Bulk)

GetBulkContainer 获取批量执行容器

func (*Client) Insert

func (c *Client) Insert(ctx context.Context, data []interface{}) error

Insert 插入数据

func (*Client) InsertOne

func (c *Client) InsertOne(ctx context.Context, data interface{}) (id string, err error)

InsertOne 单个插入

func (*Client) QueryByPaging

func (c *Client) QueryByPaging(ctx context.Context, filters bson.M, sort []string, pageIndex int64, pageSize int64, queryResult interface{}) (totalCount int64, err error)

QueryByPaging 分页查询

func (*Client) Update

func (c *Client) Update(ctx context.Context, filters bson.M, data bson.M) error

Update 更新数据

func (*Client) UpdateMany

func (c *Client) UpdateMany(ctx context.Context, filters bson.M, data bson.M) error

UpdateMany 批量更新

type ClientOptionFunc

type ClientOptionFunc func(c *Client)

func SetBasicAuth

func SetBasicAuth(username, password string) ClientOptionFunc

SetBasicAuth is 设置账号

func SetCollection

func SetCollection(coll string) ClientOptionFunc

SetCollection is 设置集合

func SetConnectTimeout

func SetConnectTimeout(t int) ClientOptionFunc

SetConnectTimeout is 设置连接超时时间

func SetDatabase

func SetDatabase(db string) ClientOptionFunc

SetDatabase is 设置db

func SetDirect

func SetDirect(enabled bool) ClientOptionFunc

SetDirect is 设置是否仅连接提供的主机

func SetHeartbeatInterval

func SetHeartbeatInterval(t int) ClientOptionFunc

SetHeartbeatInterval is 设置心跳间隔

func SetLocalThreshold

func SetLocalThreshold(t int) ClientOptionFunc

SetLocalThreshold is 设置延迟窗口

func SetMaxConnIdleTime

func SetMaxConnIdleTime(t int) ClientOptionFunc

SetMaxConnIdleTime is 设置连接最大空闲时间

func SetMaxPoolSize

func SetMaxPoolSize(size int) ClientOptionFunc

SetMaxPoolSize is 设置最大连接池数量

func SetMinPoolSize

func SetMinPoolSize(size int) ClientOptionFunc

SetMinPoolSize is 设置最小连接池数量

func SetReadConcern

func SetReadConcern(level string) ClientOptionFunc

SetReadConcern is 设置读关注等级

func SetRetryRead

func SetRetryRead(enabled bool) ClientOptionFunc

SetRetryRead is 设置是否重复读

func SetRetryWrite

func SetRetryWrite(enabled bool) ClientOptionFunc

SetRetryWrite is 设置是否重复写

func SetSocketTimeout

func SetSocketTimeout(t int) ClientOptionFunc

SetSocketTimeout is 设置读写超时时间

func SetURI

func SetURI(uri string) ClientOptionFunc

SetURI is 设置资源地址

func SetWriteConcern

func SetWriteConcern(level interface{}) ClientOptionFunc

SetWriteConcern is 设置写关注等级

type Config

type Config struct {
	URI         string `json:"uri"`
	Username    string `json:"username"`
	Password    string `json:"password"`
	Database    string `json:"database"`
	Collection  string `json:"collection"`
	MaxPoolSize uint64 `json:"max_pool_size"`
	MinPoolSize uint64 `json:"min_pool_size"`
	// 是否仅连接提供的主机,false将会发现集群其它主机
	Direct bool `json:"direct"`
	// socket读写超时时间,单位秒
	SocketTimeout time.Duration `json:"socket_timeout"`
	// 建立连接超时时间,单位秒
	ConnTimeout time.Duration `json:"conn_timeout"`
	// 空闲连接最大持续时间,单位秒
	MaxConnIdleTime time.Duration `json:"max_conn_idle_time"`
	// 心跳检查间隔,单位秒
	HeartbeatInterval time.Duration `json:"heartbeatInterval"`
	// 延迟窗口的容忍大小,单位毫秒
	LocalThreshold time.Duration `json:"local_threshold"`
	// 重复读,默认开启
	RetryRead bool `json:"retry_read"`
	// 重复写,默认开启
	RetryWrite bool `json:"retry_write"`
	// contains filtered or unexported fields
}

Config mongodb配置

Jump to

Keyboard shortcuts

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