mongodb

package
v3.4.5 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2023 License: Apache-2.0 Imports: 22 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BeforeCreate

func BeforeCreate(document any) any

BeforeCreate ...

func BeforeUpdate

func BeforeUpdate(document any) any

BeforeUpdate ...

func Caller added in v3.4.4

func Caller(skip int) map[string]string

func SetSlowThreshold added in v3.4.4

func SetSlowThreshold(threshold time.Duration)

SetSlowThreshold sets the slow threshold.

Types

type DecoratedCollection added in v3.4.4

type DecoratedCollection struct {
	*mongo.Collection

	DisableMetric bool // 关闭指标采集
	DisableTrace  bool // 关闭链路追踪
	// contains filtered or unexported fields
}

func (*DecoratedCollection) Aggregate added in v3.4.4

func (c *DecoratedCollection) Aggregate(ctx context.Context, pipeline any, result any) (err error)

Aggregate ...

func (*DecoratedCollection) Count added in v3.4.4

func (c *DecoratedCollection) Count(ctx context.Context) (result int64, err error)

Count ...

func (*DecoratedCollection) CreateIndex added in v3.4.4

func (c *DecoratedCollection) CreateIndex(ctx context.Context, key bson.D, op *options.IndexOptions) (res string, err error)

CreateIndex CreateOneIndex 创建单个普通索引

func (*DecoratedCollection) Delete added in v3.4.4

func (c *DecoratedCollection) Delete(ctx context.Context) (count int64, err error)

Delete 删除数据,并返回删除成功的数量

func (*DecoratedCollection) Drop added in v3.4.4

func (c *DecoratedCollection) Drop(ctx context.Context) (err error)

Drop ...

func (*DecoratedCollection) DropIndex added in v3.4.4

func (c *DecoratedCollection) DropIndex(ctx context.Context, name string, opts *options.DropIndexesOptions) (err error)

DropIndex 删除索引

func (*DecoratedCollection) Fields added in v3.4.4

func (c *DecoratedCollection) Fields(fields bson.M) *DecoratedCollection

Fields 指定查询字段

func (*DecoratedCollection) FindMany added in v3.4.4

func (c *DecoratedCollection) FindMany(ctx context.Context, documents any) (err error)

FindMany 查询多条数据

func (*DecoratedCollection) FindOne added in v3.4.4

func (c *DecoratedCollection) FindOne(ctx context.Context, document any) (err error)

FindOne 查询一条数据

func (*DecoratedCollection) InsertMany added in v3.4.4

func (c *DecoratedCollection) InsertMany(ctx context.Context, documents any) (val *mongo.InsertManyResult, err error)

InsertMany 写入多条数据

func (*DecoratedCollection) InsertOne added in v3.4.4

func (c *DecoratedCollection) InsertOne(ctx context.Context, document any) (val *mongo.InsertOneResult, err error)

InsertOne 写入单条数据

func (*DecoratedCollection) Limit added in v3.4.4

Limit 限制条数

func (*DecoratedCollection) ListIndexes added in v3.4.4

func (c *DecoratedCollection) ListIndexes(ctx context.Context, opts *options.ListIndexesOptions) (val []string, err error)

ListIndexes 获取所有所有

func (*DecoratedCollection) Skip added in v3.4.4

Skip 跳过条数

func (*DecoratedCollection) Sort added in v3.4.4

Sort 排序 bson.M{"created_at":-1}

func (*DecoratedCollection) UpdateMany added in v3.4.4

func (c *DecoratedCollection) UpdateMany(ctx context.Context, document any) (val *mongo.UpdateResult, err error)

UpdateMany ...

func (*DecoratedCollection) UpdateOne added in v3.4.4

func (c *DecoratedCollection) UpdateOne(ctx context.Context, document any) (val *mongo.UpdateResult, err error)

UpdateOne ...

func (*DecoratedCollection) UpdateOneRaw added in v3.4.4

func (c *DecoratedCollection) UpdateOneRaw(ctx context.Context, document any, opt ...*options.UpdateOptions) (val *mongo.UpdateResult, err error)

UpdateOneRaw 原生update

func (*DecoratedCollection) UpdateOrInsert added in v3.4.4

func (c *DecoratedCollection) UpdateOrInsert(ctx context.Context, documents []any) (val *mongo.UpdateResult, err error)

UpdateOrInsert 存在更新,不存在写入, documents 里边的文档需要有 _id 的存在

func (*DecoratedCollection) Where added in v3.4.4

Where 条件查询, bson.M{"field": "value"}

type Model added in v3.4.4

type Model struct {
	Collection *DecoratedCollection
	// contains filtered or unexported fields
}

MongoDB wraps *mongo.Client and provides a Close method.

type MongoDB

type MongoDB struct {
	*mongo.Client
	DisableMetric bool   // 关闭指标采集
	DisableTrace  bool   // 关闭链路追踪
	Name          string // 数据库名称
	Uri           string
}

MongoDB wraps *mongo.Client and provides a Close method.

func NewMongoDBClient added in v3.4.4

func NewMongoDBClient(uri string, opts ...Option) (*MongoDB, error)

NewMongoDBClient

func (*MongoDB) Close added in v3.4.2

func (cs *MongoDB) Close() error

Close disconnects the underlying *mongo.Client.

func (*MongoDB) MustNewCollection added in v3.4.4

func (m *MongoDB) MustNewCollection(collection string) *DecoratedCollection

func (*MongoDB) MustNewModel added in v3.4.4

func (m *MongoDB) MustNewModel(collection string) *Model

MustNewModel returns a Model, exits on errors.

func (*MongoDB) NewCollection added in v3.4.4

func (m *MongoDB) NewCollection(collection string) (*DecoratedCollection, error)

func (*MongoDB) NewModel added in v3.4.4

func (m *MongoDB) NewModel(collection string) (*Model, error)

func (*MongoDB) Ping added in v3.4.4

func (m *MongoDB) Ping() error

func (*MongoDB) SetDisableMetric added in v3.4.4

func (m *MongoDB) SetDisableMetric(disableMetric bool) *MongoDB

func (*MongoDB) SetDisableTrace added in v3.4.4

func (m *MongoDB) SetDisableTrace(disableTrace bool) *MongoDB

type Option added in v3.4.4

type Option func(opts *options.ClientOptions)

Option defines the method to customize a mongo model.

func WithMaxConnIdleTime added in v3.4.4

func WithMaxConnIdleTime(d time.Duration) Option

WithMaxConnIdleTime set the mon client max connection idle time.

func WithMaxPoolSize added in v3.4.4

func WithMaxPoolSize(size uint64) Option

WithMaxPoolSize set the mon client max pool size.

func WithMinPoolSize added in v3.4.4

func WithMinPoolSize(size uint64) Option

WithMinPoolSize set the mon client min pool size.

func WithTimeout added in v3.4.4

func WithTimeout(timeout time.Duration) Option

WithTimeout set the mon client operation timeout.

Jump to

Keyboard shortcuts

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