mongodb

package
v0.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 11, 2023 License: BSD-2-Clause Imports: 14 Imported by: 0

Documentation

Index

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,
		},
	}
)
View Source
var (
	RefTypeStr     = reflect.TypeOf("")
	RefTypeInt     = reflect.TypeOf(0)
	RefTypeInt32   = reflect.TypeOf(int32(0))
	RefTypeInt64   = reflect.TypeOf(int64(0))
	RefTypeFloat32 = reflect.TypeOf(float32(0))
	RefTypeFloat64 = reflect.TypeOf(float64(0))
	RefTypeBool    = reflect.TypeOf(false)
)

Functions

func Aggregate

func Aggregate(col *mongo.Collection, pipeline interface{}, result interface{}, opts ...*options.AggregateOptions) error

func BoolElem added in v0.0.4

func BoolElem() reflect.Value

func BoolSliceElem added in v0.0.4

func BoolSliceElem() reflect.Value

func Client

func Client() *mongo.Client

Client 获取客户端

func Col

func Col(name string, dbname ...string) *mongo.Collection

func Connect

func Connect() error

func DB

func DB(dbname ...string) *mongo.Database

func Disconnect

func Disconnect() error

func Elem added in v0.0.4

func Elem(v reflect.Type) reflect.Value

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 Float32Elem added in v0.0.4

func Float32Elem() reflect.Value

func Float32SliceElem added in v0.0.4

func Float32SliceElem() reflect.Value

func Float64Elem added in v0.0.4

func Float64Elem() reflect.Value

func Float64SliceElem added in v0.0.4

func Float64SliceElem() reflect.Value

func GetIncId

func GetIncId(ctx context.Context, db *mongo.Database, id string) (int64, error)

GetIncId 获取

func Int32Elem added in v0.0.4

func Int32Elem() reflect.Value

func Int32MapElem added in v0.0.4

func Int32MapElem(v reflect.Type) reflect.Value

func Int32SliceElem added in v0.0.4

func Int32SliceElem() reflect.Value

func Int64Elem added in v0.0.4

func Int64Elem() reflect.Value

func Int64MapElem added in v0.0.4

func Int64MapElem(v reflect.Type) reflect.Value

func Int64SliceElem added in v0.0.4

func Int64SliceElem() reflect.Value

func IntElem added in v0.0.4

func IntElem() reflect.Value

func IntMapElem added in v0.0.4

func IntMapElem(v reflect.Type) reflect.Value

Int Map

func IntSliceElem added in v0.0.4

func IntSliceElem() reflect.Value

func MapElem added in v0.0.4

func MapElem(k, v reflect.Type) reflect.Value

反射创建MAP (以 .Addr.Interface() 获取指针数据)

func Ptr added in v0.0.4

func Ptr(v reflect.Type) reflect.Value

反射创建结构体 (以 .Addr.Interface() 获取指针数据)

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 通过反射查询单条记录

func SliceElem added in v0.0.4

func SliceElem(v reflect.Type) reflect.Value

反射创建切片 (以 .Addr.Interface() 获取指针数据)

func SliceTwoElem added in v0.0.4

func SliceTwoElem(v reflect.Type) reflect.Value

反射创建切片 (以 .Addr.Interface() 获取指针数据)

func StrElem added in v0.0.4

func StrElem() reflect.Value

func StrMapElem added in v0.0.4

func StrMapElem(v reflect.Type) reflect.Value

Str Map

func StrSliceElem added in v0.0.4

func StrSliceElem() reflect.Value

Types

type AutoIncId

type AutoIncId struct {
	Id  string `bson:"_id" json:"id"`
	Num int64  `bson:"n" json:"n"`
}

type Option

type Option func(o *Options)

func WithConnectTimeout

func WithConnectTimeout(t time.Duration) Option

func WithDbName

func WithDbName(db string) Option

func WithMaxConnIdleTime

func WithMaxConnIdleTime(t time.Duration) Option

func WithMaxPoolSize

func WithMaxPoolSize(size uint64) Option

func WithMinPoolSize

func WithMinPoolSize(size uint64) Option

func WithReadWriteTimeout

func WithReadWriteTimeout(t time.Duration) Option

func WithSocketTimeout

func WithSocketTimeout(t time.Duration) Option

func WithUrl

func WithUrl(url string) Option

type Options

type Options struct {
	RawUrl           string
	DbName           string
	MinPoolSize      uint64        // 最小连接池大小
	MaxPoolSize      uint64        // 最大连接池大小
	ConnectTimeout   time.Duration // 连接超时时间
	SocketTimeout    time.Duration
	MaxConnIdleTime  time.Duration // 最大空闲时间
	ReadWriteTimeout time.Duration // 读写超时时间
}

func (*Options) Init

func (o *Options) Init(opts ...Option)

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 NewScan

func NewScan(count, page, size int64) *Scan

func (*Scan) FindOptions

func (sc *Scan) FindOptions() *options.FindOptions

type Store

type Store struct {
	// contains filtered or unexported fields
}

MongoDB 数据存储

func NewStore

func NewStore(opts ...Option) *Store

func S

func S() *Store

func (*Store) C

func (s *Store) C(name string, dbname ...string) *mongo.Collection

Collection 获取集合对象

func (*Store) Client

func (s *Store) Client() *mongo.Client

Client 获取客户端

func (*Store) CloneC

func (s *Store) CloneC(name string, dbname ...string) (*mongo.Collection, error)

CloneCollection 克隆集合对象

func (*Store) Connect

func (s *Store) Connect() error

func (*Store) D

func (s *Store) D(dbname ...string) *mongo.Database

Database 获取数据库对象

func (*Store) DbName

func (s *Store) DbName() string

func (*Store) Disconnect

func (s *Store) Disconnect() error

func (*Store) GetIncId

func (s *Store) GetIncId(id string) (int64, error)

func (*Store) Init

func (s *Store) Init(opts ...Option)

func (*Store) ListCollectionNames

func (s *Store) ListCollectionNames(dbname ...string) ([]string, error)

获取集合列表

func (*Store) Opts

func (s *Store) Opts() *Options

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
}

func NewTable

func NewTable(name, pkField, fkField string, model interface{}) *Table

func (*Table) AddIndex

func (dt *Table) AddIndex(index []mongo.IndexModel)

func (*Table) Data

func (dt *Table) Data() []interface{}

func (*Table) FkField

func (dt *Table) FkField() string

func (*Table) FkKind

func (dt *Table) FkKind() string

func (*Table) Index

func (dt *Table) Index() []mongo.IndexModel

func (*Table) Model

func (dt *Table) Model() reflect.Type

func (*Table) Name

func (dt *Table) Name() string

func (*Table) PkField

func (dt *Table) PkField() string

func (*Table) PkKind

func (dt *Table) PkKind() string

func (*Table) SetData

func (dt *Table) SetData(data []interface{})

func (*Table) SetIndex

func (dt *Table) SetIndex(index []mongo.IndexModel)

type Tables

type Tables struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewTables

func NewTables(dbname string) *Tables

实例化数据库模型

func (*Tables) Add

func (mts *Tables) Add(name, pkField, fkField string, model interface{}, index []mongo.IndexModel, data []interface{}) *Table

Add 添加新集合

func (*Tables) Check

func (mts *Tables) Check(mdb *Store) error

Init 初始化数据库

func (*Tables) Count

func (mts *Tables) Count() int

Count 返回集合数量

func (*Tables) DbName

func (mts *Tables) DbName() string

func (*Tables) Get

func (mts *Tables) Get(name string) *Table

Get 获取指定集合

func (*Tables) SetAutoIdData

func (mts *Tables) SetAutoIdData(data []interface{})

func (*Tables) SetDbName

func (mts *Tables) SetDbName(dbname string)

func (*Tables) Tables

func (mts *Tables) Tables() map[string]*Table

Tables 返回所有数据表

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL