Documentation ¶
Index ¶
- Constants
- Variables
- func Client(name ClientName) *mongo.Client
- func DefaultCtxWithOut() (context.Context, context.CancelFunc)
- func GetCompanyCollName(companyID, coll string) string
- func InitClient(c *Config) (*mongo.Client, error)
- type AggregateOptions
- type Auth
- type ClientName
- type Config
- type CountOptions
- type Cursor
- type Dao
- func (d *Dao) Aggregate(ctx context.Context, pipeline interface{}, opts ...*options.AggregateOptions) (*Cursor, error)
- func (d *Dao) CountDocuments(ctx context.Context, filter interface{}, opts ...*options.CountOptions) (int64, error)
- func (d *Dao) DecodeOne(err error) error
- func (d *Dao) DefaultCtx() (context.Context, context.CancelFunc)
- func (d *Dao) DeleteMany(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*DeleteResult, error)
- func (d *Dao) DeleteOne(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*DeleteResult, error)
- func (d *Dao) Distinct(ctx context.Context, fieldName string, filter interface{}, ...) ([]interface{}, error)
- func (d *Dao) Find(ctx context.Context, filter interface{}, opts ...*options.FindOptions) (*Cursor, error)
- func (d *Dao) FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) *SingleResult
- func (d *Dao) FindOneAndDelete(ctx context.Context, filter interface{}, ...) *SingleResult
- func (d *Dao) FindOneAndUpdate(ctx context.Context, filter interface{}, update interface{}, ...) *SingleResult
- func (d *Dao) HasDuplicatedError(err error) bool
- func (d *Dao) InsertMany(ctx context.Context, documents []interface{}, ...) (*InsertManyResult, error)
- func (d *Dao) InsertOne(ctx context.Context, document interface{}, opts ...*options.InsertOneOptions) (*InsertOneResult, error)
- func (d *Dao) Name() string
- func (d *Dao) UpdateMany(ctx context.Context, filter interface{}, update interface{}, ...) (*UpdateResult, error)
- func (d *Dao) UpdateOne(ctx context.Context, filter interface{}, update interface{}, ...) (*UpdateResult, error)
- func (d *Dao) UseSession(ctx context.Context, fn func(mongo.SessionContext) error) error
- type DeleteOptions
- type DeleteResult
- type FindOneAndDeleteOptions
- type FindOneAndUpdateOptions
- type FindOneOptions
- type FindOptions
- type InsertManyOptions
- type InsertManyResult
- type InsertOneOptions
- type InsertOneResult
- type MgoConn
- type SessionContext
- type SingleResult
- type UpdateOptions
- type UpdateResult
- type WriteException
Constants ¶
const ( DefaultConnectTimeout time.Duration = 5 // mongo connect 建立mongodb连接默认超时时间 5s DefaultQueryTimeout time.Duration = 5 // mongo query 查询默认超时时间 5s )
默认超时时间配置
Variables ¶
var ErrNoDocuments = mongo.ErrNoDocuments
ErrNoDocuments is returned by Decode when an operation that returns a SingleResult doesn't return any documents.
Functions ¶
func Client ¶
func Client(name ClientName) *mongo.Client
Client 根据name获取client 注意:本方法直接返回client结果,是因为传入的name一定是写好的constant,而且一定已经成功初始化完成,否则程序不可能执行到这里,所以不用做任何判断
func DefaultCtxWithOut ¶
func DefaultCtxWithOut() (context.Context, context.CancelFunc)
DefaultCtxWithOut 带超时
func GetCompanyCollName ¶
GetCompanyCollName 获取企业分库表名
Types ¶
type Auth ¶
type Auth struct { Username string `id:"username" default:"foo"` // username Password string `id:"password" default:"bar"` // password AuthSource string `id:"source" default:"admin"` // auth source PasswordSet bool `id:"on" default:"true"` // is auth on }
Auth mongodb认证信息结构
type ClientName ¶
type ClientName string
ClientName mongodb client名称枚举
const ( MKBiz ClientName = "markting" MKWat ClientName = "mk_wat" WP ClientName = "wp" )
ClientName 营销平台数据库集群连接名称
type Dao ¶
type Dao struct { ClientName ClientName DBName string CollName string CompanyID string // contains filtered or unexported fields }
Dao 数据库操作结构
func NewDao ¶
func NewDao(clientName ClientName, dbName string, collName string, companyID string) (d *Dao)
NewDao 新建数据库操作对象
func (*Dao) Aggregate ¶
func (d *Dao) Aggregate(ctx context.Context, pipeline interface{}, opts ...*options.AggregateOptions) (*Cursor, error)
Aggregate 聚合查询
func (*Dao) CountDocuments ¶
func (d *Dao) CountDocuments(ctx context.Context, filter interface{}, opts ...*options.CountOptions) (int64, error)
CountDocuments https://godoc.org/go.mongodb.org/mongo-driver/mongo#Collection.CountDocuments
func (*Dao) DefaultCtx ¶
func (d *Dao) DefaultCtx() (context.Context, context.CancelFunc)
DefaultCtx 默认上下文环境生成
func (*Dao) DeleteMany ¶
func (d *Dao) DeleteMany(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*DeleteResult, error)
DeleteMany https://godoc.org/go.mongodb.org/mongo-driver/mongo#Collection.DeleteMany
func (*Dao) DeleteOne ¶
func (d *Dao) DeleteOne(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*DeleteResult, error)
DeleteOne https://godoc.org/go.mongodb.org/mongo-driver/mongo#Collection.DeleteOne
func (*Dao) Distinct ¶
func (d *Dao) Distinct(ctx context.Context, fieldName string, filter interface{}, opts ...*options.DistinctOptions) ([]interface{}, error)
Distinct https://github.com/mongodb/mongo-go-driver/blob/master/mongo/collection.go
func (*Dao) Find ¶
func (d *Dao) Find(ctx context.Context, filter interface{}, opts ...*options.FindOptions) (*Cursor, error)
Find https://godoc.org/go.mongodb.org/mongo-driver/mongo#Collection.Find
func (*Dao) FindOne ¶
func (d *Dao) FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) *SingleResult
FindOne https://godoc.org/go.mongodb.org/mongo-driver/mongo#Collection.FindOne
func (*Dao) FindOneAndDelete ¶
func (d *Dao) FindOneAndDelete(ctx context.Context, filter interface{}, opts ...*options.FindOneAndDeleteOptions) *SingleResult
FindOneAndDelete 删除一个
func (*Dao) FindOneAndUpdate ¶
func (d *Dao) FindOneAndUpdate(ctx context.Context, filter interface{}, update interface{}, opts ...*options.FindOneAndUpdateOptions) *SingleResult
FindOneAndUpdate 更新一个
func (*Dao) HasDuplicatedError ¶
HasDuplicatedError 是否是重复id写入错误
func (*Dao) InsertMany ¶
func (d *Dao) InsertMany(ctx context.Context, documents []interface{}, opts ...*options.InsertManyOptions) (*InsertManyResult, error)
InsertMany https://godoc.org/go.mongodb.org/mongo-driver/mongo#Collection.InsertMany
func (*Dao) InsertOne ¶
func (d *Dao) InsertOne(ctx context.Context, document interface{}, opts ...*options.InsertOneOptions) (*InsertOneResult, error)
InsertOne https://godoc.org/go.mongodb.org/mongo-driver/mongo#Collection.InsertOne
func (*Dao) Name ¶
Name https://godoc.org/go.mongodb.org/mongo-driver/mongo#Collection.Name
func (*Dao) UpdateMany ¶
func (d *Dao) UpdateMany(ctx context.Context, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*UpdateResult, error)
UpdateMany https://github.com/mongodb/mongo-go-driver/blob/master/mongo/collection.go#L548
func (*Dao) UpdateOne ¶
func (d *Dao) UpdateOne(ctx context.Context, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*UpdateResult, error)
UpdateOne https://github.com/mongodb/mongo-go-driver/blob/master/mongo/collection.go#L532
func (*Dao) UseSession ¶
UseSession 开启默认事务
type FindOneAndDeleteOptions ¶
type FindOneAndDeleteOptions = options.FindOneAndDeleteOptions
FindOneAndDeleteOptions 删除一个
type FindOneAndUpdateOptions ¶
type FindOneAndUpdateOptions = options.FindOneAndUpdateOptions
FindOneAndUpdateOptions 更新一个
type InsertManyOptions ¶
type InsertManyOptions = options.InsertManyOptions
InsertManyOptions insertMany