Documentation
¶
Index ¶
- Constants
- Variables
- func NewClient(address string, opts ...*options.ClientOptions) (client *mongo.Client, err error)
- func NewClientOptions() *options.ClientOptions
- func UpdateOne(tx *DB, coll *mongo.Collection, filter clause.Filter, data update.Update, ...) (err error)
- type BulkWrite
- func (this *BulkWrite) Delete(where ...interface{})
- func (this *BulkWrite) Insert(documents ...interface{})
- func (this *BulkWrite) Options(opts ...*options.BulkWriteOptions)
- func (this *BulkWrite) Result() *mongo.BulkWriteResult
- func (this *BulkWrite) Save(data any, where ...any)
- func (this *BulkWrite) SetUpdateFilter(filter BulkWriteUpdateFilter)
- func (this *BulkWrite) Size() int
- func (this *BulkWrite) Submit() (err error)
- func (this *BulkWrite) Update(data any, where ...any)
- type BulkWriteUpdateFilter
- type Cache
- func (this *Cache) Cursor(update int64, filter CacheFilter) []any
- func (this *Cache) Delete(id string)
- func (this *Cache) Get(id string) any
- func (this *Cache) Has(id string) (ok bool)
- func (this *Cache) Len() int
- func (this *Cache) Listener(t CacheEventType, id string, update int64)
- func (this *Cache) Lock(f func() error) error
- func (this *Cache) Page(page *Paging, filter CacheFilter) (err error)
- func (this *Cache) Range(f func(any) bool)
- func (this *Cache) Reload(ts int64, handle ...CacheHandle) error
- type CacheData
- type CacheEventType
- type CacheFilter
- type CacheHandle
- type CacheModel
- type CacheSetter
- type Config
- type DB
- func (db *DB) AutoMigrator(dst ...interface{}) error
- func (db *DB) BulkWrite(model any, filter ...BulkWriteUpdateFilter) *BulkWrite
- func (db *DB) Close() (err error)
- func (db *DB) Collection(model any) (tx *DB, coll *mongo.Collection)
- func (db *DB) Count(count interface{}, conds ...interface{}) (tx *DB)
- func (db *DB) Create(value interface{}) (tx *DB)
- func (db *DB) Database(dbname string) *DB
- func (db *DB) Delete(conds ...interface{}) (tx *DB)
- func (db *DB) Errorf(format interface{}, args ...interface{}) *DB
- func (db *DB) Find(val any, where ...any) (tx *DB)
- func (db *DB) First(val any, where ...any) (tx *DB)
- func (db *DB) Inc(key string, val int) (tx *DB)
- func (db *DB) IncludeZeroValue() (tx *DB)
- func (db *DB) Last(val any, where ...any) (tx *DB)
- func (db *DB) Limit(limit int) (tx *DB)
- func (db *DB) Model(value any, modify ...bool) (tx *DB)
- func (db *DB) Multiple() (tx *DB)
- func (db *DB) ObjectID() primitive.ObjectID
- func (db *DB) Omit(columns ...string) (tx *DB)
- func (db *DB) Order(key string, value int) (tx *DB)
- func (db *DB) Page(paging *Paging, where ...any) (tx *DB)
- func (db *DB) Query(val any, where ...any) (tx *DB)
- func (db *DB) Save(values any, conds ...any) (tx *DB)
- func (db *DB) Select(columns ...string) (tx *DB)
- func (db *DB) Session(session *Session) *DB
- func (db *DB) Set(key string, val any) (tx *DB)
- func (db *DB) SetColumn(data map[string]interface{}) (err error)
- func (db *DB) Start(dbname string, address interface{}) (err error)
- func (db *DB) Table(name string) (tx *DB)
- func (db *DB) Take(val any, where ...any) (tx *DB)
- func (db *DB) Update(values any, conds ...any) (tx *DB)
- func (db *DB) Updates(values any, conds ...any) (tx *DB)
- func (db *DB) Upsert() (tx *DB)
- func (db *DB) Where(query interface{}, args ...interface{}) (tx *DB)
- func (db *DB) WithContext(ctx context.Context) *DB
- type ModelBulkWriteFilter
- type Paging
- type Session
- type Statement
Constants ¶
const ( MongoTagName = "BuildUpdate" MongoPrimaryKey = "_id" MongoSetOnInsert = "$setOnInsert" )
const DBNameUpdate = "update"
const DefaultPageSize = 1000
Variables ¶
var ( ErrInvalidConfig = errors.New("invalid config") // ErrInvalidTransaction invalid transaction when you are trying to `Commit` or `Rollback` ErrInvalidTransaction = errors.New("invalid transaction") // ErrNotImplemented not implemented ErrNotImplemented = errors.New("not implemented") // ErrMissingWhereClause missing where clause ErrMissingWhereClause = errors.New("WHERE conditions required") // ErrUnsupportedRelation unsupported relations ErrUnsupportedRelation = errors.New("unsupported relations") // ErrPrimaryKeyRequired primary keys required ErrPrimaryKeyRequired = errors.New("primary key required") // ErrModelValueRequired model value required ErrModelValueRequired = errors.New("model value required") // ErrInvalidData unsupported data ErrInvalidData = errors.New("unsupported data") // ErrUnsupportedDriver unsupported driver ErrUnsupportedDriver = errors.New("unsupported driver") // ErrRegistered registered ErrRegistered = errors.New("registered") // ErrInvalidField invalid field ErrInvalidField = errors.New("invalid field") // ErrEmptySlice empty slice found ErrEmptySlice = errors.New("empty slice found") // ErrDryRunModeUnsupported dry run mode unsupported ErrDryRunModeUnsupported = errors.New("dry run mode unsupported") // ErrInvalidDB invalid db ErrInvalidDB = errors.New("invalid db") // ErrInvalidValue invalid value ErrInvalidValue = errors.New("invalid value, should be pointer to struct or slice") ErrInvalidModel = errors.New("invalid model, should be pointer to struct or struct") // ErrInvalidValueOfLength invalid values do not match length ErrInvalidValueOfLength = errors.New("invalid association values, length doesn't match") ErrSelectOnOmitsExist = errors.New("select on omits exist") ErrOmitOnSelectsExist = errors.New("omit on selects exist") )
Functions ¶
func NewClient ¶
NewClient
uri实例 mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[dbname][?options]]
mongodb:// 前缀,代表这是一个Connection String
username:password@ 如果启用了鉴权,需要指定用户密码
hostX:portX 多个 mongos 的地址列表
/dbname 鉴权时,用户帐号所属的数据库
?options 指定额外的连接选项
read preference
1)primary : 主节点,默认模式,读操作只在主节点,如果主节点不可用,报错或者抛出异常。 2)primaryPreferred:首选主节点,大多情况下读操作在主节点,如果主节点不可用,如故障转移,读操作在从节点。 3)secondary:从节点,读操作只在从节点, 如果从节点不可用,报错或者抛出异常。 4)secondaryPreferred:首选从节点,大多情况下读操作在从节点,特殊情况(如单主节点架构)读操作在主节点。 5)nearest:最邻近节点,读操作在最邻近的成员,可能是主节点或者从节点。
func NewClientOptions ¶ added in v0.0.3
func NewClientOptions() *options.ClientOptions
Types ¶
type BulkWrite ¶
type BulkWrite struct {
// contains filtered or unexported fields
}
func (*BulkWrite) Options ¶ added in v0.0.3
func (this *BulkWrite) Options(opts ...*options.BulkWriteOptions)
func (*BulkWrite) Result ¶ added in v0.0.3
func (this *BulkWrite) Result() *mongo.BulkWriteResult
func (*BulkWrite) SetUpdateFilter ¶ added in v1.1.0
func (this *BulkWrite) SetUpdateFilter(filter BulkWriteUpdateFilter)
type BulkWriteUpdateFilter ¶ added in v1.1.0
type Cache ¶ added in v1.1.0
type Cache struct {
// contains filtered or unexported fields
}
func NewCache ¶ added in v1.1.0
func NewCache(handle CacheHandle) *Cache
func (*Cache) Cursor ¶ added in v1.1.0
func (this *Cache) Cursor(update int64, filter CacheFilter) []any
func (*Cache) Listener ¶ added in v1.1.0
func (this *Cache) Listener(t CacheEventType, id string, update int64)
Listener 监听数据库变化 id 变更数据ID update 变化时间
type CacheData ¶ added in v1.1.0
type CacheData struct {
// contains filtered or unexported fields
}
func NewCacheData ¶ added in v1.1.0
func NewCacheData() *CacheData
type CacheEventType ¶ added in v1.1.0
type CacheEventType int8
const ( CacheEventTypeCreate CacheEventType = 0 CacheEventTypeUpdate CacheEventType = 1 CacheEventTypeDelete CacheEventType = 2 )
type CacheFilter ¶ added in v1.1.0
type CacheFilter func(v CacheModel) any //返回nil 过滤失败
type CacheHandle ¶ added in v1.1.0
type CacheHandle interface {
Reload(ts int64, cb CacheSetter) error
}
type CacheModel ¶ added in v1.1.0
type CacheModel interface {
GetUpdate() int64
}
type CacheSetter ¶ added in v1.1.0
type CacheSetter func(k any, v CacheModel)
type DB ¶
type DB struct { *Config Error error RowsAffected int64 //操作影响的条数 // contains filtered or unexported fields }
DB GORM DB definition
func (*DB) AutoMigrator ¶
AutoMigrator returns migrator Sparse
func (*DB) BulkWrite ¶
func (db *DB) BulkWrite(model any, filter ...BulkWriteUpdateFilter) *BulkWrite
BulkWrite 批量写入
func (*DB) Collection ¶
func (db *DB) Collection(model any) (tx *DB, coll *mongo.Collection)
func (*DB) Delete ¶
Delete 删除记录 db.delete(&User{Id:1,name:"myname"}) 匹配 _id=1 db.model(&User).delete(1) 匹配 _id=1 db.model(&User).delete([]int{1,2,3}) 匹配 _id IN (1,2,3) db.model(&User).delete("name = ?","myname") 匹配 name=myname
func (*DB) IncludeZeroValue ¶ added in v1.1.0
IncludeZeroValue 设置Update Updates 时包含零值 使用 Save 时自动包含零值
func (*DB) Model ¶
Model specify the model you would like to run db operations
// update all users's name to `hello` db.model(&User{}).Update("name", "hello") // if user's primary key is non-blank, will use it as condition, then will only update the user's name to `hello` db.model(&user).Update("name", "hello")
func (*DB) Order ¶
Order specify order when retrieve records from dbname Order 排序方式 1 和 -1 来指定排序的方式,其中 1 为升序排列,而 -1 是用于降序排列。
func (*DB) Query ¶ added in v1.1.0
Query get records that match given conditions value must be a pointer to a slice
func (*DB) SetColumn ¶
SetColumn set column's value to model
stmt.SetColumn("Name", "jinzhu") // Hooks Method
func (*DB) Updates ¶ added in v1.1.0
Updates 更新多列 Updates 方法支持 struct 和 map[string]interface{} 参数。当使用 struct 更新时,默认情况下只会更新非零值的字段 如果您想要在更新时选择、忽略某些字段,您可以使用 Select、Omit 自动关闭 updateAndModify
type ModelBulkWriteFilter ¶ added in v1.1.0
type Paging ¶
type Paging struct { //order []bson.E //排序 Rows interface{} `json:"rows"` Page int `json:"page"` //当前页 Size int `json:"size"` //每页大小 Total int `json:"total"` //总页码数 Record int `json:"record"` //总记录数 Update int64 `json:"update"` //最后更新时间 }
Paging 分页
type Session ¶
type Session struct { DBName string //DryRun bool //PrepareStmt bool //NewDB bool //SkipHooks bool //SkipDefaultTransaction bool //DisableNestedTransaction bool //AllowGlobalUpdate bool //FullSaveAssociations bool //QueryFields bool Context context.Context }
Session session config when create session with Session() method