Documentation ¶
Index ¶
- Variables
- type Base
- func (db *Base) Client() Client
- func (db *Base) Collection() string
- func (db *Base) Count(ctx context.Context, selector interface{}) (count int, err error)
- func (db *Base) Database() string
- func (db *Base) DeleteAll(ctx context.Context, selector interface{}) (changeInfo *mgo.ChangeInfo, err error)
- func (db *Base) DeleteId(ctx context.Context, id string) error
- func (db *Base) DeleteOne(ctx context.Context, selector interface{}) error
- func (db *Base) Distinct(ctx context.Context, selector interface{}, key string) ([]interface{}, error)
- func (db *Base) Do(ctx context.Context, f func(c *mgo.Collection) error) error
- func (db *Base) FindAll(ctx context.Context, selector interface{}, sort []string, picker []string, ...) error
- func (db *Base) FindId(ctx context.Context, id string, picker []string, result interface{}) error
- func (db *Base) FindOne(ctx context.Context, selector interface{}, picker []string, result interface{}) error
- func (db *Base) FindOneWithSort(ctx context.Context, selector interface{}, sort []string, picker []string, ...) error
- func (db *Base) Indexes() ([]mgo.Index, error)
- func (db *Base) Insert(ctx context.Context, doc ...interface{}) error
- func (db *Base) PipeAll(ctx context.Context, pipeline interface{}, ret interface{}) error
- func (db *Base) RemoveAll(ctx context.Context, selector interface{}) (changeInfo *mgo.ChangeInfo, err error)
- func (db *Base) RemoveId(ctx context.Context, id string) error
- func (db *Base) RemoveOne(ctx context.Context, selector interface{}) error
- func (db *Base) UpdateAll(ctx context.Context, selector, update interface{}) (changeInfo *mgo.ChangeInfo, err error)
- func (db *Base) UpdateId(ctx context.Context, id string, update interface{}) error
- func (db *Base) UpdateOne(ctx context.Context, selector, update interface{}) error
- func (db *Base) Upsert(ctx context.Context, selector, update interface{}) (changeInfo *mgo.ChangeInfo, err error)
- func (db *Base) UpsertId(ctx context.Context, id string, update interface{}) (changeInfo *mgo.ChangeInfo, err error)
- type Client
- type Config
- type Manager
- type Model
Constants ¶
This section is empty.
Variables ¶
Functions ¶
This section is empty.
Types ¶
type Base ¶
type Base struct {
// contains filtered or unexported fields
}
func (*Base) Collection ¶
func (*Base) Distinct ¶
func (db *Base) Distinct(ctx context.Context, selector interface{}, key string) ([]interface{}, error)
Distinct unmarshals into result the list of distinct values for the given key.
For example:
ret, err = db.Distinct(bson.M{"gender": 1}, "age") fmt.Println(ret)
DB:
{ ObjectId("603a081694ea2e906792a8f1"), name:"a", gender:"1", age:12 } { ObjectId("603a081694ea2e906792a8f2"), name:"b", gender:"1", age:13 } { ObjectId("603a081694ea2e906792a8f3"), name:"c", gender:"1", age:14 } { ObjectId("603a081694ea2e906792a8f4"), name:"d", gender:"1", age:15 } { ObjectId("603a081694ea2e906792a8f5"), name:"e", gender:"1", age:14 } { ObjectId("603a081694ea2e906792a8f6"), name:"f", gender:"1", age:13 }
Console:
[12, 13, 14 ,15]
func (*Base) Do ¶
Do it is used for you to use the nativer mgoc interface according to your own needs, Use when you can't find the method you want in this package
func (*Base) FindOne ¶
func (db *Base) FindOne(ctx context.Context, selector interface{}, picker []string, result interface{}) error
FindOne the param picker([]string) represents the field to return
func (*Base) FindOneWithSort ¶
type Client ¶
type Client interface { Kernel() *mgo.Session Close() Do(ctx context.Context, model Model, exec func(s *mgo.Collection) error) error Config() Config NewBaseModel(database string, collection string) *Base }
func Dial ¶
Dial example: mongodb://username:password@localhost:27017,otherhost:27017/db, default auto_time is true
func GetGlobalClient ¶
func GetGlobalClient() Client
func NewGlobalClient ¶
NewGlobalClient If there is only one Mongo, you can select the global client
type Config ¶
type Config struct { Addrs []string `mapstructure:"addrs" json:"addrs"` Database string `mapstructure:"db" json:"db"` Username string `mapstructure:"username" json:"username"` Password string `mapstructure:"password" json:"password"` Source string `mapstructure:"source" json:"source"` 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" json:"mode"` PoolLimit int `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"` // Alias If it is empty, it will not be included in the client Map management // 如果为空则不纳入clientMap管理 Alias string `mapstructure:"alias" json:"alias"` }
Click to show internal directories.
Click to hide internal directories.