mongo

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2021 License: Apache-2.0 Imports: 16 Imported by: 3

Documentation

Overview

MongoDB 连接 * 查找单个文档时, 如果未找到文件, 则会返回 ErrNoDocuments 错误 * 查找多个文档时, 如果未找到任何文档, 则会返回 ErrNilDocument 错误 * bson.M 是无序的 doc 描述 * bson.D 是有序的 doc 描述 * bsonx.Doc 是类型安全的 doc 描述

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,
		},
	}
)

Functions

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 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 GetIncId

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

GetIncId 获取

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 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
}

MongoDB集合

func NewTable

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

func (*Table) AddIndex

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

AddIndex 追加索引

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)

AddIndex 设置索引

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