mongo

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2021 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotMatched       = errors.New("not matched")
	ErrInstanceNotFound = errors.New("mongodb instance db not found")
	ErrNoDocuments      = mongo.ErrNoDocuments
)
View Source
var Indexes []Index

Functions

func CreateIndex

func CreateIndex(indexes []Index)

将要创建索引占存

func IsDuplicateKey

func IsDuplicateKey(err error) bool

func NewMongoClient

func NewMongoClient(ctx context.Context, opts ...*options.ClientOptions) (*mongo.Client, error)

NewMongoClient client and ping

func NewMongoClientByURI

func NewMongoClientByURI(ctx context.Context, uri string) (*mongo.Client, error)

NewMongoClientByURI uri = mongo://...

func URIToHosts

func URIToHosts(uri string) []string

URIToHosts parse to hosts

Types

type CollStatsResp

type CollStatsResp struct {
	Ok             int              `json:"ok"`
	Ns             string           `json:"ns"`
	Size           int64            `json:"size"`
	Count          int64            `json:"count"`
	AvgObjSize     int64            `json:"avgObjSize"`
	StorageSize    int64            `json:"storageSize"`
	Capped         bool             `json:"capped"`
	TotalIndexSize int64            `json:"totalIndexSize"`
	IndexSizes     map[string]int64 `json:"indexSizes"`
}

type Command

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

func NewCommand

func NewCommand(db *Database) *Command

func (*Command) CollStats

func (c *Command) CollStats(ctx context.Context, collection string) (CollStatsResp, error)

func (*Command) DBStats

func (c *Command) DBStats(ctx context.Context) (DBStatsResp, error)

func (*Command) HostInfo

func (c *Command) HostInfo(ctx context.Context) (HostInfoResp, error)

type Config

type Config struct {
	Conns []Conn
}

type Conn

type Conn struct {
	Database string `defval:"test"`
	URI      string `defval:"mongodb://127.0.0.1:27017/admin" null:""`
}

type DBStatsResp

type DBStatsResp struct {
	Ok          int32  `json:"ok"`
	DB          string `json:"db"`
	Collections int32  `json:"collections"`
	Objects     int64  `json:"objects"`
	DataSize    int64  `json:"dataSize"`
	StorageSize int64  `json:"storageSize"`
	Indexes     int64  `json:"indexes"`
	IndexSize   int64  `json:"indexSize"`
}

type DataBaseInterface

type DataBaseInterface interface {
	// 扩展的快捷方法
	Exists(ctx context.Context, collection string, filter interface{}) (bool, error)
	FindOne(ctx context.Context, collection string, filter interface{}, doc interface{}, opt ...*options.FindOneOptions) (bool, error)
	Find(ctx context.Context, collection string, filter bson.M, docs interface{}, opt ...*options.FindOptions) error
	FindCount(ctx context.Context, collection string, filter interface{}, docs interface{}, findOpt *options.FindOptions, countOpt *options.CountOptions) (int64, error)
	ListCollectionNames(ctx context.Context, prefix ...string) ([]string, error)
	UpsertCollectionIndexMany(indexMany ...[]Index) error

	// 原有方法
	Client() *mongo.Client
	Name() string
	Collection(name string, opts ...*options.CollectionOptions) *mongo.Collection
	Aggregate(ctx context.Context, pipeline interface{},
		opts ...*options.AggregateOptions) (*mongo.Cursor, error)
	RunCommand(ctx context.Context, runCommand interface{}, opts ...*options.RunCmdOptions) *mongo.SingleResult
	RunCommandCursor(ctx context.Context, runCommand interface{}, opts ...*options.RunCmdOptions) (*mongo.Cursor, error)
	Drop(ctx context.Context) error
	ListCollectionSpecifications(ctx context.Context, filter interface{},
		opts ...*options.ListCollectionsOptions) ([]*mongo.CollectionSpecification, error)
	ListCollections(ctx context.Context, filter interface{}, opts ...*options.ListCollectionsOptions) (*mongo.Cursor, error)
	ReadConcern() *readconcern.ReadConcern
	ReadPreference() *readpref.ReadPref
	WriteConcern() *writeconcern.WriteConcern
	Watch(ctx context.Context, pipeline interface{},
		opts ...*options.ChangeStreamOptions) (*mongo.ChangeStream, error)
	CreateCollection(ctx context.Context, name string, opts ...*options.CreateCollectionOptions) error
	CreateView(ctx context.Context, viewName, viewOn string, pipeline interface{},
		opts ...*options.CreateViewOptions) error
}

type Database

type Database struct {
	*mongo.Database
}

Database packaging some method, shortcut

func NewDatabase

func NewDatabase(ctx context.Context, uri, database string) (*Database, error)

func (*Database) Exists

func (db *Database) Exists(ctx context.Context, collection string, filter interface{}) (bool, error)

func (*Database) Find

func (db *Database) Find(ctx context.Context, collection string, filter bson.M, docs interface{}, opt ...*options.FindOptions) error

func (*Database) FindCount

func (db *Database) FindCount(ctx context.Context, collection string, filter interface{}, docs interface{}, findOpt *options.FindOptions, countOpt *options.CountOptions) (int64, error)

func (*Database) FindOne

func (db *Database) FindOne(ctx context.Context, collection string, filter interface{}, doc interface{}, opt ...*options.FindOneOptions) (bool, error)

func (*Database) ListCollectionNames

func (db *Database) ListCollectionNames(ctx context.Context, prefix ...string) ([]string, error)

func (*Database) UpsertCollectionIndexMany

func (db *Database) UpsertCollectionIndexMany(indexMany ...[]Index) error

type Groups

type Groups struct {
	Config *Config
	// contains filtered or unexported fields
}

Groups Mongodb 群组实例

func NewGroups

func NewGroups(cfg *Config) (*Groups, error)

NewCluster 简单的多实例管理

func (*Groups) Disconnect

func (g *Groups) Disconnect() error

func (*Groups) GetInstance

func (g *Groups) GetInstance(database string) (*Database, error)

GetInstance 通过库名 获取实例

type HostInfoResp

type HostInfoResp struct {
	System map[string]interface{} `json:"system"`
	Os     map[string]interface{} `json:"os"`
}

type Index

type Index struct {
	Collection         string
	Name               string // 指定索引名称
	Keys               bson.D
	Unique             bool  // 唯一索引
	Background         bool  // 非阻塞创建索引
	ExpireAfterSeconds int32 // 多少秒后过期
}

func (Index) Validate

func (i Index) Validate() error

type ShardCollection

type ShardCollection struct {
	Prefix string
	Sep    string // 分隔符
	// contains filtered or unexported fields
}

ShardCollection 分片集合名

func NewShardCollection

func NewShardCollection(prefix string, day int) ShardCollection

prefix 集合名前缀 day>=31 | day<=0 则按月分片, day = x 则每 x 天为一个分片区间

func (ShardCollection) CollNameByStartEnd

func (sc ShardCollection) CollNameByStartEnd(bucket string, start, end int64) []string

CollNameByStartEnd 根据开始时间和结束时间,查询出所有生成的 name

func (ShardCollection) CollNameDate

func (sc ShardCollection) CollNameDate(collName string) (time.Time, error)

func (ShardCollection) DaySpan

func (sc ShardCollection) DaySpan() map[int]int

func (ShardCollection) DecodeCollName

func (sc ShardCollection) DecodeCollName(collName string) (prefix, bucket string, year int, month time.Month, span int, err error)

func (ShardCollection) EncodeCollName

func (sc ShardCollection) EncodeCollName(bucket string, timestamp int64) string

func (ShardCollection) SepTime

func (sc ShardCollection) SepTime(collName string) (t time.Time, err error)

SepTime 当前集合时间分区内下一个分隔时间

Jump to

Keyboard shortcuts

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