Documentation ¶
Overview ¶
MongoDB 连接 * 查找单个文档时, 如果未找到文件, 则会返回 ErrNoDocuments 错误 * 查找多个文档时, 如果未找到任何文档, 则会返回 ErrNilDocument 错误 * bson.M 是无序的 doc 描述 * bson.D 是有序的 doc 描述 * bsonx.Doc 是类型安全的 doc 描述
Index ¶
- Constants
- Variables
- func Client() *mongo.Client
- func Col(name string, dbname ...string) *mongo.Collection
- func Connect() error
- func DB(dbname ...string) *mongo.Database
- func Disconnect() error
- func FindAll(col *mongo.Collection, filter interface{}, result interface{}, ...) error
- func FindOne(col *mongo.Collection, filter interface{}, result interface{}, ...) error
- func GetIncId(ctx context.Context, db *mongo.Database, id string) (int64, error)
- func SelectAll(col *mongo.Collection, filter interface{}, model reflect.Type, ...) ([]interface{}, error)
- func SelectOne(col *mongo.Collection, filter interface{}, model reflect.Type, ...) (interface{}, error)
- type AutoIncId
- type Option
- func WithConnectTimeout(t time.Duration) Option
- func WithDbName(db string) Option
- func WithMaxConnIdleTime(t time.Duration) Option
- func WithMaxPoolSize(size uint64) Option
- func WithMinPoolSize(size uint64) Option
- func WithReadWriteTimeout(t time.Duration) Option
- func WithSocketTimeout(t time.Duration) Option
- func WithUrl(url string) Option
- type Options
- type Scan
- type Store
- func (s *Store) C(name string, dbname ...string) *mongo.Collection
- func (s *Store) Client() *mongo.Client
- func (s *Store) CloneC(name string, dbname ...string) (*mongo.Collection, error)
- func (s *Store) Connect() error
- func (s *Store) D(dbname ...string) *mongo.Database
- func (s *Store) DbName() string
- func (s *Store) Disconnect() error
- func (s *Store) GetIncId(id string) (int64, error)
- func (s *Store) Init(opts ...Option)
- func (s *Store) ListCollectionNames(dbname ...string) ([]string, error)
- func (s *Store) Opts() *Options
- func (s *Store) Scan(dbName, tabName string, cur, size int64, filter interface{}, ...) *Scan
- type Table
- func (dt *Table) AddIndex(index []mongo.IndexModel)
- func (dt *Table) Data() []interface{}
- func (dt *Table) FkField() string
- func (dt *Table) FkKind() string
- func (dt *Table) Index() []mongo.IndexModel
- func (dt *Table) Model() reflect.Type
- func (dt *Table) Name() string
- func (dt *Table) PkField() string
- func (dt *Table) PkKind() string
- func (dt *Table) SetData(data []interface{})
- func (dt *Table) SetIndex(index []mongo.IndexModel)
- type Tables
- func (mts *Tables) Add(name, pkField, fkField string, model interface{}, index []mongo.IndexModel, ...) *Table
- func (mts *Tables) Check(mdb *Store) error
- func (mts *Tables) Count() int
- func (mts *Tables) DbName() string
- func (mts *Tables) Get(name string) *Table
- func (mts *Tables) SetAutoIdData(data []interface{})
- func (mts *Tables) SetDbName(dbname string)
- func (mts *Tables) Tables() map[string]*Table
Constants ¶
View Source
const ( DefaultConnectTimeout = 5 * time.Second // 连接超时时间 DefaultSocketTimeout = 5 * time.Second DefaultMaxConnIdleTime = 3 * time.Second // 最大空闲时间 DefaultReadWriteTimeout = 3 * time.Second // 读写超时时间 )
View Source
const AutoIncIdName = "auto_inc_id"
Variables ¶
View Source
var ( Opts = &struct { MongoUrl string // MongoDB Proxy URI地址 MongoMinPool uint64 // MongoDB 最小连接数 MongoMaxPool uint64 // MongoDB 最大连接数 }{} Flags = []cli.Flag{ &cli.StringFlag{ Name: "mongo", Value: "", Usage: "设置MongoDB连接地址. 格式: [username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]]", EnvVars: []string{"GAME_MONGO_URL"}, Destination: &Opts.MongoUrl, }, &cli.Uint64Flag{ Name: "mongo_min_pool", Value: 20, Usage: "设置MongoDB最小连接数", EnvVars: []string{"GAME_MONGO_MIN_POOL"}, Destination: &Opts.MongoMinPool, }, &cli.Uint64Flag{ Name: "mongo_max_pool", Value: 100, Usage: "设置MongoDB最大连接数", EnvVars: []string{"GAME_MONGO_MAX_POOL"}, Destination: &Opts.MongoMaxPool, }, } )
Functions ¶
func Disconnect ¶
func Disconnect() error
func FindAll ¶
func FindAll(col *mongo.Collection, filter interface{}, result interface{}, options ...*options.FindOptions) error
func FindOne ¶
func FindOne(col *mongo.Collection, filter interface{}, result interface{}, options ...*options.FindOneOptions) error
func SelectAll ¶
func SelectAll(col *mongo.Collection, filter interface{}, model reflect.Type, options ...*options.FindOptions) ([]interface{}, error)
SelectAll 通过反射查询多条记录
func SelectOne ¶
func SelectOne(col *mongo.Collection, filter interface{}, model reflect.Type, options ...*options.FindOneOptions) (interface{}, error)
SelectOne 通过反射查询单条记录
Types ¶
type Option ¶
type Option func(o *Options)
func WithConnectTimeout ¶
func WithDbName ¶
func WithMaxConnIdleTime ¶
func WithMaxPoolSize ¶
func WithMinPoolSize ¶
func WithReadWriteTimeout ¶
func WithSocketTimeout ¶
type Options ¶
type Scan ¶
type Scan struct { Page int64 `json:"page"` // 当前页数 Count int64 `json:"count"` // 总数量 Size int64 `json:"size"` // 每页大小 Offset int64 `json:"-"` // 跳过 }
func FindScan ¶
func FindScan(ctx context.Context, col *mongo.Collection, page, size int64, filter interface{}, result interface{}, fn ...func(opts *options.FindOptions) *options.FindOptions) *Scan
分段获取数据
func (*Scan) FindOptions ¶
func (sc *Scan) FindOptions() *options.FindOptions
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
MongoDB 数据存储
func (*Store) C ¶
func (s *Store) C(name string, dbname ...string) *mongo.Collection
Collection 获取集合对象
func (*Store) Disconnect ¶
func (*Store) ListCollectionNames ¶
获取集合列表
func (*Store) Scan ¶
func (s *Store) Scan(dbName, tabName string, cur, size int64, filter interface{}, result interface{}, fn ...func(opts *options.FindOptions) *options.FindOptions) *Scan
type Table ¶
type Table struct {
// contains filtered or unexported fields
}
MongoDB集合
func (*Table) Index ¶
func (dt *Table) Index() []mongo.IndexModel
Click to show internal directories.
Click to hide internal directories.