mongoc

package
v0.0.0-...-a2093d6 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2022 License: LGPL-2.1 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FilterNil = bson.M{}

Functions

This section is empty.

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 Base

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

func NewBaseModel

func NewBaseModel(client Client, database string, collection string) *Base

NewBaseModel You can quickly get the initialized Client through ClientManager().Get(alias string), if you use the global Client, you can use GetGlobalClient() to get Client. 可以通过 ClientManager().Get(alias string) 快速获取已初始化的Client, 若使用的全局Client, 则可使用 GetGlobalClient()

func (*Base) Aggregate

func (model *Base) Aggregate(ctx context.Context, pipeline, results interface{}, opts ...*options.AggregateOptions) error

func (*Base) BulkWrite

func (model *Base) BulkWrite(ctx context.Context, models []mongo.WriteModel, opts ...*options.BulkWriteOptions) (*mongo.BulkWriteResult, error)

func (*Base) Client

func (model *Base) Client() Client

func (*Base) Clone

func (model *Base) Clone(opts ...*options.CollectionOptions) (*mongo.Collection, error)

func (*Base) Collection

func (model *Base) Collection() string

func (*Base) Count

func (model *Base) Count(ctx context.Context, filter interface{}, opts ...*options.CountOptions) (int64, error)

func (*Base) CountDocuments

func (model *Base) CountDocuments(ctx context.Context, filter interface{}, opts ...*options.CountOptions) (int64, error)

func (*Base) Database

func (model *Base) Database() string

func (*Base) DeleteId

func (model *Base) DeleteId(ctx context.Context, id string, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)

func (*Base) DeleteMany

func (model *Base) DeleteMany(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)

func (*Base) DeleteOne

func (model *Base) DeleteOne(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)

func (*Base) Distinct

func (model *Base) Distinct(ctx context.Context, fieldName string, filter interface{}, opts ...*options.DistinctOptions) ([]interface{}, error)

func (*Base) Do

func (model *Base) Do(ctx context.Context, exec Exec) (interface{}, error)

func (*Base) DoWithSession

func (model *Base) DoWithSession(ctx context.Context, exec SessionExec) error

func (*Base) DoWithTransaction

func (model *Base) DoWithTransaction(ctx context.Context, exec ModelExec) (interface{}, error)

func (*Base) Drop

func (model *Base) Drop(ctx context.Context) error

func (*Base) EstimatedDocumentCount

func (model *Base) EstimatedDocumentCount(ctx context.Context, opts ...*options.EstimatedDocumentCountOptions) (int64, error)

func (*Base) Find

func (model *Base) Find(ctx context.Context, filter interface{}, results interface{}, opts ...*options.FindOptions) error

func (*Base) FindId

func (model *Base) FindId(ctx context.Context, id string, result interface{}, opts ...*options.FindOneOptions) error

func (*Base) FindOne

func (model *Base) FindOne(ctx context.Context, filter interface{}, result interface{}, opts ...*options.FindOneOptions) error

func (*Base) FindOneAndDelete

func (model *Base) FindOneAndDelete(ctx context.Context, filter interface{}, result interface{}, opts ...*options.FindOneAndDeleteOptions) error

func (*Base) FindOneAndReplace

func (model *Base) FindOneAndReplace(ctx context.Context, filter, replacement, result interface{}, opts ...*options.FindOneAndReplaceOptions) error

func (*Base) FindOneAndUpdate

func (model *Base) FindOneAndUpdate(ctx context.Context, filter, update, result interface{}, opts ...*options.FindOneAndUpdateOptions) error

func (*Base) FindOneAndUpsert

func (model *Base) FindOneAndUpsert(ctx context.Context, filter, update, result interface{}, opts ...*options.FindOneAndUpdateOptions) error

func (*Base) Indexes

func (model *Base) Indexes() mongo.IndexView

func (*Base) InsertMany

func (model *Base) InsertMany(ctx context.Context, documents []interface{}, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error)

func (*Base) InsertOne

func (model *Base) InsertOne(ctx context.Context, document interface{}, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error)

func (*Base) ReplaceOne

func (model *Base) ReplaceOne(ctx context.Context, filter, replacement interface{}, opts ...*options.ReplaceOptions) (*mongo.UpdateResult, error)

func (*Base) UpdateId

func (model *Base) UpdateId(ctx context.Context, id string, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)

func (*Base) UpdateMany

func (model *Base) UpdateMany(ctx context.Context, filter, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)

func (*Base) UpdateOne

func (model *Base) UpdateOne(ctx context.Context, filter, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)

func (*Base) Upsert

func (model *Base) Upsert(ctx context.Context, filter, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)

func (*Base) UpsertId

func (model *Base) UpsertId(ctx context.Context, id string, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)

func (*Base) Watch

func (model *Base) Watch(ctx context.Context, pipeline interface{}, opts ...*options.ChangeStreamOptions) (*mongo.ChangeStream, error)

type Client

type Client interface {
	Kernel() *mongo.Client
	Close(ctx context.Context) error
	Config() Config
	Do(ctx context.Context, model Model, exec Exec) (interface{}, error)
	DoWithTransaction(ctx context.Context, model Model, exec Exec) (interface{}, error)
	DoWithSession(ctx context.Context, model Model, exec SessionExec) error
	NewBaseModel(database string, collection string) *Base
}

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, options options.ClientOptions, alias string) (Client, error)

func NewClientWithURI

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

NewClientWithURI uri format: mongodb://username:password@example1.com,example2.com,example3.com/?replicaSet=test&w=majority&wtimeoutMS=5000 Alias is used to specify the client alias. When used, the client can be obtained from the clientMap of the manger through the alias, and ClientManager().Get(alias) can be called alias用于指定client别名,使用时可以通过该别名从manger的clientMap中获取client, 调用 ClientManager().Get(alias)

func NewGlobalClient

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

NewGlobalClient If there is only one Mongo, you can select the global 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"`
	//AutoTime    bool          `mapstructure:"auto_time"`
	Alias string `mapstructure:"alias" json:"alias"`
}

type Exec

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

type Manager

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

func ClientManager

func ClientManager() Manager

type Model

type Model interface {
	Database() string
	Collection() string
}

type ModelExec

type ModelExec = func(ctx context.Context, model *Base) (interface{}, error)

type SessionExec

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

Jump to

Keyboard shortcuts

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