Documentation ¶
Index ¶
- Constants
- type Client
- func (f *Client) AddMetricsLabelConverter(c func(label string) (newLabel string, hit bool))
- func (f *Client) Client() *mongo.Client
- func (f *Client) DoTransaction(ctx context.Context, ...) (res interface{}, err error)
- func (f *Client) NewCollectionWrapper(database, collection string) CollectionWrapper
- func (f *Client) ScanCursor(ctx context.Context, cursor *mongo.Cursor, result interface{}) (err error)
- func (f *Client) Timeout() time.Duration
- type CollectionWrapper
- type CollectionWrapperBase
- type CollectionWrapperGeneric
- type Config
Constants ¶
const ( DEFAULT_POOLSIZE = 3 DEFAULT_SOCKET_TIMEOUT = 10 * time.Second )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func InitClient ¶
func InitClient(cfg Config, opts ...*options.ClientOptions) (client *Client, err error)
func (*Client) AddMetricsLabelConverter ¶
AddMetricsLabelConverter
添加db、collection的转换逻辑,用于向Prometheus上报metrics时的lable处理,防止分库分表的存储导致label过多。
依次执行所有converter,直到返回hit=ture且newLabel不为空。
func (*Client) DoTransaction ¶
func (f *Client) DoTransaction(ctx context.Context, fn func(sessCtx mongo.SessionContext) ( interface{}, error), opts ...*options.TransactionOptions) (res interface{}, err error)
DoTransaction is helper to exec transaction.
Ref: https://www.mongodb.com/docs/drivers/go/current/fundamentals/transactions/
1. opts can be:
wc := writeconcern.New(writeconcern.WMajority())
opts := options.Transaction().SetWriteConcern(wc)
2. ATTENTION:
mongo commonds in fn must use sessCtx as its ctx param
func (*Client) NewCollectionWrapper ¶
func (f *Client) NewCollectionWrapper(database, collection string) CollectionWrapper
NewCollectionWrapper get collection operation wrapper, with a default waitPoolTimeout valued one second.
func (*Client) ScanCursor ¶
type CollectionWrapper ¶
type CollectionWrapper interface { CollectionWrapperBase // Find Find(ctx context.Context, filter interface{}, result interface{}, sort []string, skip, limit int64, opts ...*options.FindOptions) (err error) // FindOne FindOne(ctx context.Context, filter interface{}, result interface{}, sort []string, skip int64, opts ...*options.FindOneOptions) (has bool, err error) // FindID FindID(ctx context.Context, ID interface{}, result interface{}, opts ...*options.FindOneOptions) (has bool, err error) // FindOneAndUpdate FindOneAndUpdate(ctx context.Context, filter, update, result interface{}, sort []string, upsert, returnNew bool, opts ...*options.FindOneAndUpdateOptions) (has bool, err error) // FindOneAndReplace FindOneAndReplace(ctx context.Context, filter, replacement, result interface{}, sort []string, upsert, returnNew bool, opts ...*options.FindOneAndReplaceOptions) (has bool, err error) // FindOneAndDelete FindOneAndDelete(ctx context.Context, filter, result interface{}, sort []string, opts ...*options.FindOneAndDeleteOptions) (has bool, err error) // InsertOne InsertOne(ctx context.Context, document interface{}, opts ...*options.InsertOneOptions) (insertedID interface{}, err error) // InsertMany InsertMany(ctx context.Context, document []interface{}, opts ...*options.InsertManyOptions) (insertedIDs []interface{}, err error) }
CollectionWrapper declares a wrapper of mongo collection operators.
type CollectionWrapperBase ¶
type CollectionWrapperBase interface { // Collection get official mongo.Collection Collection() *mongo.Collection // GenSortBson translate sort keys like [-_id, cnt, +ut] to bson.D GenSortBson(sort []string) (result bson.D) // FindCursor 返回官方的cursor,注意通过这个cursor读取数据,会脱离metrics监控。只有当需要读取大量数据,Find会超时时,才用FindCursor。 FindCursor(ctx context.Context, filter interface{}, sort []string, skip, limit int64, opts ...*options.FindOptions) (cursor *mongo.Cursor, err error) // UpdateOne UpdateOne(ctx context.Context, filter, update interface{}, upsert bool, opts ...*options.UpdateOptions) (result *mongo.UpdateResult, err error) // UpdateID UpdateID(ctx context.Context, ID, update interface{}, upsert bool, opts ...*options.UpdateOptions) (result *mongo.UpdateResult, err error) // UpdateMany UpdateMany(ctx context.Context, filter, update interface{}, upsert bool, opts ...*options.UpdateOptions) (result *mongo.UpdateResult, err error) // Counts Count(ctx context.Context, filter interface{}, skip, limit int64, opts ...*options.CountOptions) (count int64, err error) // EstimatedCount,For a fast count of the documents in the collection EstimatedCount(ctx context.Context, opts ...*options.EstimatedDocumentCountOptions) (count int64, err error) // DeleteOne DeleteOne(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (has bool, err error) // DeleteID DeleteID(ctx context.Context, ID interface{}, opts ...*options.DeleteOptions) (has bool, err error) // DeleteMany DeleteMany(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (deletedCnt int64, err error) // Distinct Distinct(ctx context.Context, filedName string, filter interface{}, opts ...*options.DistinctOptions) (result []interface{}, err error) // BulkWrite BulkWrite(ctx context.Context, models []mongo.WriteModel, opts ...*options.BulkWriteOptions) (result *mongo.BulkWriteResult, err error) // Aggregate // pipeline: a slice of aggragate commands // result: a slice address Aggregate(ctx context.Context, pipeline, result interface{}, opts ...*options.AggregateOptions) (err error) /*UseSession provides the ability of transaction. * fn is a closure, all the action in it should use SessionContext as context param. * Use OfficialClient.GenSessionWrapper() is a good idea to simple your code. */ UseSession(ctx context.Context, fn func(mongo.SessionContext) error, opts ...*options.SessionOptions) (err error) }
type CollectionWrapperGeneric ¶
type CollectionWrapperGeneric[T any] interface { CollectionWrapperBase // Find Find(ctx context.Context, filter interface{}, sort []string, skip, limit int64, opts ...*options.FindOptions) (result []T, err error) // FindOne FindOne(ctx context.Context, filter interface{}, sort []string, skip int64, opts ...*options.FindOneOptions) (result T, has bool, err error) // FindID FindID(ctx context.Context, ID interface{}, opts ...*options.FindOneOptions) (result T, has bool, err error) // FindOneAndUpdate FindOneAndUpdate(ctx context.Context, filter, update interface{}, sort []string, upsert, returnNew bool, opts ...*options.FindOneAndUpdateOptions) (result T, has bool, err error) // FindOneAndReplace FindOneAndReplace(ctx context.Context, filter, replacement interface{}, sort []string, upsert, returnNew bool, opts ...*options.FindOneAndReplaceOptions) (result T, has bool, err error) // FindOneAndDelete FindOneAndDelete(ctx context.Context, filter interface{}, sort []string, opts ...*options.FindOneAndDeleteOptions) (result T, has bool, err error) // InsertOne InsertOne(ctx context.Context, document T, opts ...*options.InsertOneOptions) (insertedID interface{}, err error) // InsertMany InsertMany(ctx context.Context, document []T, opts ...*options.InsertManyOptions) (insertedIDs []interface{}, err error) }
CollectionWrapperGeneric 基于泛型对Find、Insert类操作进行重写
func NewCollectionWrapper ¶
func NewCollectionWrapper[T any](client *Client, database, collection string) CollectionWrapperGeneric[T]
type Config ¶
type Config struct { Hostport string `json:"hostport" ini:"hostport" yaml:"hostport"` UserName string `json:"username" ini:"username" yaml:"username"` Password string `json:"password" ini:"password" yaml:"password"` Poolsize int `json:"poolsize" ini:"poolsize" yaml:"poolsize"` Timeout int `json:"timeout" ini:"timeout" yaml:"timeout"` SecondaryPreferred bool `json:"enable_secondary_preferred" ini:"enable_secondary_preferred" yaml:"enable_secondary_preferred"` }