mongodb

package
v0.0.0-...-2692626 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2023 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StatusNormal   uint32 = 0 // 正常状态
	StatusAbnormal uint32 = 1 // 异常状态
)
View Source
const ErrorKeyConnectFailure string = "ErrorKeyMongoDBConnectFailure" //Mongo 连接 失败
View Source
const ErrorKeyDisconnectFailure string = "ErrorKeyDisconnectFailure" //Mongo 断开连接 失败
View Source
const ErrorKeyOperateFailure string = "ErrorKeyMongoDBOperateFailure" //Mongo 操作 失败
View Source
const ErrorKeyReadFailure string = "ErrorKeyMongoDBReadFailure" //Mongo 读 失败
View Source
const ErrorKeyWriteFailure string = "ErrorKeyMongoDBWriteFailure" //Mongo 写 失败
View Source
const FuncIDUpdateMany uint32 = 0x302
View Source
const FuncIDUpdateOne uint32 = 0x301

Variables

View Source
var MaxConnIdleTimeDefault = time.Minute * 5
View Source
var MaxConnectingDefault uint64 = 4 // 设置为 P/2
View Source
var MaxPoolSizeDefault uint64 = 24 // 可以设置为 2*P + 30% 的数量
View Source
var MinPoolSizeDefault uint64 = 8 // 可以设置为 P 的数量
View Source
var TimeoutDurationDefault = time.Minute

Functions

func CountDocuments

func CountDocuments(arg ...interface{}) (count interface{}, err error)

CountDocuments 获取文档总数

参数:
	0:context.Context
	1:*mongo.Collection
	2:time.Duration
	3:filter interface{}
返回值:
	count int64 数量

func CreateBulkWriteModel

func CreateBulkWriteModel(funcArg *FunctionArg) mongo.WriteModel

CreateBulkWriteModel FunctionArg转换为BulkWrite的model

func EndTransaction

func EndTransaction(sessionContext mongo.SessionContext, err error)

EndTransaction 结束MongoDB事务

func Find

func Find(arg ...interface{}) ([]bson.M, error)

Find 查找所有文档

参数:
	0:context.Context
	1:*mongo.Collection
	2:time.Duration
	3:filter interface{}
	[4:]:opts ...[]*options.FindOptions
返回值:
	[]bson.M 多个文档数据

func FindOne

func FindOne(arg ...interface{}) (exist interface{}, err error)

FindOne 查找一个文档/查找一个空文档

参数:
	0:context.Context
	1:*mongo.Collection
	2:time.Duration
	3:filter interface{}
	4:record interface{}
	[5:]:opts ...[]*options.FindOneOptions
返回值:
	exist bool 是否有 true:有, false:无

func InsertOne

func InsertOne(arg ...interface{}) (interface{}, error)

InsertOne 插入文档

参数:
	0:context.Context
	1:*mongo.Collection
	2:time.Duration
	3:document interface{}
	[!]不支持[4:]:opts ...*options.InsertOneOptions

func NextValue

func NextValue(arg ...interface{}) (singleResult interface{}, err error)

NextValue 返回更新后的值

参数:
	0:context.Context
	1:*mongo.Collection
	2:time.Duration
	3:filter interface{}
	4:update interface{}
返回值:
	singleResult *mongo.SingleResult

func StartTransaction

func StartTransaction(sessionContext mongo.SessionContext) error

StartTransaction 开启MongoDB事务

func UpdateMany

func UpdateMany(arg ...interface{}) (updateResult interface{}, err error)

UpdateMany

参数:
	0:context.Context
	1:*mongo.Collection
	2:time.Duration
	3:filter interface{}
	4:update interface{}
	[5:]:opts ...*options.UpdateOptions(目前只支持一个opt-ArrayFilters)
返回值:
	updateResult *mongo.UpdateResult

func UpdateOne

func UpdateOne(arg ...interface{}) (updateResult interface{}, err error)

UpdateOne

参数:
	0:context.Context
	1:*mongo.Collection
	2:time.Duration
	3:filter interface{}
	4:update interface{}
	[5:]:opts ...*options.UpdateOptions(目前只支持一个 opt-ArrayFilters)
返回值:
	updateResult *mongo.UpdateResult

Types

type FunctionArg

type FunctionArg struct {
	// contains filtered or unexported fields
}

FunctionArg 函数,参数

func CreateFunctionArg

func CreateFunctionArg(collectionName string, collection *mongo.Collection, model mongo.WriteModel, expiration time.Duration) (functionArg *FunctionArg)

CreateFunctionArg BulkWrite的model转换为FunctionArg

func NewFunctionArg

func NewFunctionArg(fun libfuncmgr.Function, funcID uint32, collectionName string, arg ...interface{}) *FunctionArg

NewFunctionArg 构造新的FunctionArg

参数:
	arg:
		0:context.Context
		1:*mongo.Collection
		2:time.Duration
		...
		参考 InsertOne, UpdateOne, UpdateMany

func (*FunctionArg) AppendArg

func (p *FunctionArg) AppendArg(i interface{})

func (*FunctionArg) GetArg

func (p *FunctionArg) GetArg() (arg []interface{})

func (*FunctionArg) GetCollectionName

func (p *FunctionArg) GetCollectionName() string

func (*FunctionArg) GetFuncID

func (p *FunctionArg) GetFuncID() uint32

func (*FunctionArg) GetTimeOut

func (p *FunctionArg) GetTimeOut() time.Duration

func (*FunctionArg) Invoke

func (p *FunctionArg) Invoke() (interface{}, error)

Invoke 调用函数

type Mgr

type Mgr struct {
	// contains filtered or unexported fields
}

func (*Mgr) Connect

func (p *Mgr) Connect(ctx context.Context, opts ...*Options) error

Connect 连接

func (*Mgr) Disconnect

func (p *Mgr) Disconnect(ctx context.Context) error

Disconnect 断开链接

func (*Mgr) DropDatabase

func (p *Mgr) DropDatabase() error

func (*Mgr) EnableSharding

func (p *Mgr) EnableSharding(ctx context.Context, dbName string) error

EnableSharding 启用分片

func (*Mgr) GetClient

func (p *Mgr) GetClient() *mongo.Client

func (*Mgr) GetTimeOutDuration

func (p *Mgr) GetTimeOutDuration() time.Duration

GetTimeOutDuration 超时时间

func (*Mgr) IndexesCreateOne

func (p *Mgr) IndexesCreateOne(ctx context.Context, collection *mongo.Collection, field string, order int, unique bool) (newIndexName string, err error)

IndexesCreateOne 创建索引

参数:
	field:索引字段
	order:[1:ascending order 按升序创建索引. -1:descending order 按降序来创建索引]
	unique: 建立的索引是否唯一.指定为true创建唯一索引.默认值为false

func (*Mgr) IsAbnormal

func (p *Mgr) IsAbnormal() bool

IsAbnormal 是否异常 false:正常 true:异常

func (*Mgr) SetAbnormal

func (p *Mgr) SetAbnormal(abnormal bool)

SetAbnormal 设置异常 false:正常 true:异常

func (*Mgr) ShardCollectionHash

func (p *Mgr) ShardCollectionHash(ctx context.Context, dbName string, collectionName string, key string, numInitialChunks int) error

ShardCollectionHash 分片数据集-hash

参数:
	numInitialChunks:chuck数量 e.g.:1024

func (*Mgr) SwitchedCollection

func (p *Mgr) SwitchedCollection(name string) *mongo.Collection

func (*Mgr) SwitchedDatabase

func (p *Mgr) SwitchedDatabase(name string)

type Options

type Options struct {
	// contains filtered or unexported fields
}

Options contains options to configure a server instance. Each option can be set through setter functions. See documentation for each setter function for an explanation of the option.

func NewOptions

func NewOptions() *Options

NewOptions 新的Options

func (*Options) WithAddrs

func (p *Options) WithAddrs(addrs []string) *Options

func (*Options) WithDBName

func (p *Options) WithDBName(dbName *string) *Options

func (*Options) WithMaxConnIdleTime

func (p *Options) WithMaxConnIdleTime(maxConnIdleTime *time.Duration) *Options

func (*Options) WithMaxConnecting

func (p *Options) WithMaxConnecting(maxConnecting *uint64) *Options

func (*Options) WithMaxPoolSize

func (p *Options) WithMaxPoolSize(maxPoolSize *uint64) *Options

func (*Options) WithMinPoolSize

func (p *Options) WithMinPoolSize(minPoolSize *uint64) *Options

func (*Options) WithPW

func (p *Options) WithPW(pw string) *Options

func (*Options) WithTimeoutDuration

func (p *Options) WithTimeoutDuration(timeoutDuration *time.Duration) *Options

func (*Options) WithUserName

func (p *Options) WithUserName(userName string) *Options

Jump to

Keyboard shortcuts

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