Documentation
¶
Index ¶
- Variables
- func NewAutoTimeModel(client *Client, database string, collection string) *autoTimeModel
- func NewBaseModel(client *Client, database string, collection string) *baseModel
- type Auth
- type Client
- func GetGlobalClient() *Client
- func NewClient(ctx context.Context, config *Config) (*Client, error)
- func NewClientWithOptions(ctx context.Context, opts ...*options.ClientOptions) (*Client, error)
- func NewClientWithURI(ctx context.Context, uri string) (*Client, error)
- func NewGlobalClient(ctx context.Context, config *Config) (*Client, error)
- type Config
- type IManager
- type Model
Constants ¶
This section is empty.
Variables ¶
var FilterAll = bson.M{}
Functions ¶
func NewAutoTimeModel ¶ added in v1.4.1
func NewBaseModel ¶
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func GetGlobalClient ¶
func GetGlobalClient() *Client
func NewClient ¶
NewClient If only one db is used, it is recommended to use: NewGlobalClient 若只使用到了一个库,推荐使用: NewGlobalClient
func NewClientWithOptions ¶
func NewClientWithURI ¶
NewClientWithURI uri format: mongodb://username:password@example1.com,example2.com,example3.com/?replicaSet=test&w=majority&wtimeoutMS=5000
func NewGlobalClient ¶
NewGlobalClient If there is only one Mongo, you can select the global client
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,则事务自动提交, 否则回滚
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 IManager ¶ added in v1.4.0
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
FindWithCursor(ctx context.Context, filter interface{}, results interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error)
}