mongoc

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2022 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FilterAll = bson.M{}

Functions

func NewModel added in v1.4.0

func NewModel(client *Client, database string, collection string) *model

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 Exec) (interface{}, error)

func (*Client) DoWithSession

func (c *Client) DoWithSession(ctx context.Context, model Model, exec SessionExec) 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 Exec) (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 added in v1.4.0

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

type Exec

type Exec = func(ctx context.Context, collection *mongo.Collection) (interface{}, error)

type IManager added in v1.4.0

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 {
	Database() string
	Collection() string
}

type SessionExec

type SessionExec = func(sessionCtx mongo.SessionContext, collection *mongo.Collection) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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