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() (err error)
- func (this *BulkWrite) Update(data interface{}, where ...interface{})
- type Config
- type DB
- func (db *DB) AutoMigrator(dst ...interface{}) error
- func (db *DB) BulkWrite(model interface{}) *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) 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) Select(columns ...string) (tx *DB)
- func (db *DB) Session(session *Session) *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) Update(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 Paging
- type Session
- type Statement
Constants ¶
const ( MongoTagName = "BuildUpdate" MongoPrimaryKey = "_id" MongoSetOnInsert = "$setOnInsert" )
const DBNameUpdate = "update"
const DefaultPageSize = 100
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
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) 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) 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) SetColumn ¶
SetColumn set column's value to model
stmt.SetColumn("Name", "jinzhu") // Hooks Method
type Paging ¶
type Paging struct { Rows interface{} `json:"rows"` Page int `json:"page"` //当前页 Size int `json:"size"` //每页大小 Total int `json:"total"` //总页码数 Record int `json:"record"` //总记录数 Update int64 `json:"update"` //最后更新时间 // contains filtered or unexported fields }
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