mongoc

package
v1.4.10 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2023 License: Apache-2.0 Imports: 12 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FilterAll = bson.M{}

Functions

func NewModelAutoTime added in v1.4.10

func NewModelAutoTime(client *Client, database string, collection string) *modelAutoTime

func NewModelBase added in v1.4.10

func NewModelBase(client *Client, database string, collection string) *modelBase

Types

type Auth

type Auth struct {
	Username string `mapstructure:"username" json:"username"`
	Password string `mapstructure:"password" json:"password"`
	Source   string `mapstructure:"source" json:"source"`
}

type Client

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

func GetGlobalClient

func GetGlobalClient() *Client

func NewClient

func NewClient(ctx context.Context, config Config) (*Client, error)

NewClient If only one db is used, it is recommended to use: NewGlobalClient 若只使用到了一个库,推荐使用: NewGlobalClient

func NewClientWithOptions

func NewClientWithOptions(ctx context.Context, opts ...*options.ClientOptions) (*Client, error)

func NewClientWithURI

func NewClientWithURI(ctx context.Context, uri string) (*Client, error)

NewClientWithURI uri format: mongodb://username:password@example1.com,example2.com,example3.com/?replicaSet=test&w=majority&wtimeoutMS=5000

func NewGlobalClient

func NewGlobalClient(ctx context.Context, config Config) (*Client, error)

NewGlobalClient If there is only one Mongo, you can select the global client

func (*Client) Do

func (c *Client) Do(ctx context.Context, model Model, exec func(ctx context.Context, model Model) (interface{}, error)) (interface{}, error)

func (*Client) DoWithSession

func (c *Client) DoWithSession(ctx context.Context, model Model, exec func(sessionCtx mongo.SessionContext, model Model) error) error

DoWithSession To open a transaction in the form of Seesion, you need to manually commit the transaction by sessionContext.CommitTransaction(ctx) or rollback the transaction by sessionContext.CommitTransaction(ctx) 以Seesion形式开启事务, 需要手动sessionContext.CommitTransaction(ctx)提交事务或sessionContext.CommitTransaction(ctx)回滚事务

func (*Client) DoWithTransaction

func (c *Client) DoWithTransaction(ctx context.Context, model Model, exec func(ctx context.Context, model Model) (interface{}, error)) (interface{}, error)

DoWithTransaction Execute the transaction, if the return err is nil, the transaction is automatically committed, otherwise it is rolled back 执行事务, 如果返回err为nil,则事务自动提交, 否则回滚

func (*Client) Kernel

func (c *Client) Kernel() *mongo.Client

type Config

type Config struct {
	Addrs          []string `mapstructure:"addrs" json:"addrs"`
	Auth           *Auth    `mapstructure:"auth" json:"auth"`
	ReplicaSetName string   `mapstructure:"replica_set_name" json:"replica_set_name"`
	// Timeout the unit is seconds
	Timeout time.Duration `mapstructure:"timeout" json:"timeout"`
	// Mode      mgo.Mode      `mapstructure:"mode"`
	PoolLimit uint64 `mapstructure:"pool_limit" json:"pool_limit"`
	// MaxIdleTime The maximum number of seconds to remain idle in the pool, default 20 minutes
	MaxIdleTime time.Duration `mapstructure:"max_idle_time" json:"max_idle_time"`
	AppName     string        `mapstructure:"app_name" json:"app_name"`
}

func (Config) Convert2Options

func (c Config) Convert2Options() *options.ClientOptions

type IManager

type IManager interface {
	Add(alias string, c *Client)
	Delete(alias string)
	Get(alias string) (*Client, error)
	Has(alias string) bool
	Clear()
}

func Manager

func Manager() IManager

type Model

type Model interface {
	Kernel() *mongo.Collection
	Database() string
	Collection() string
	InsertOne(ctx context.Context, document interface{}, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error)
	InsertMany(ctx context.Context, documents []interface{}, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error)
	DeleteOne(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
	DeleteMany(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
	FindOne(ctx context.Context, filter interface{}, result interface{}, opts ...*options.FindOneOptions) error
	FindMany(ctx context.Context, filter interface{}, results interface{}, opts ...*options.FindOptions) error
	UpdateOne(ctx context.Context, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
	UpdateMany(ctx context.Context, filter, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
	Count(ctx context.Context, filter interface{}, opts ...*options.CountOptions) (int64, error)
	Distinct(ctx context.Context, fieldName string, filter interface{}, opts ...*options.DistinctOptions) ([]interface{}, error)
	Do(ctx context.Context, exec func(ctx context.Context, model Model) (interface{}, error)) (interface{}, error)
	DoWithTransaction(ctx context.Context, exec func(ctx context.Context, model Model) (interface{}, error)) (interface{}, error)
	DoWithSession(ctx context.Context, exec func(sessionCtx mongo.SessionContext, model Model) error) error
	FindCursor(ctx context.Context, filter interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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