mongo

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2021 License: MIT Imports: 14 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)

CreateIndex add indexes to Indexes, waiting create

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

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

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

func NewCommand added in v0.1.1

func NewCommand(db *Database) *Command

func (*Command) CollStats added in v0.1.1

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

func (*Command) DBStats added in v0.1.1

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

func (*Command) HostInfo added in v0.1.1

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

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
	Transaction(ctx context.Context, tx func(sessCtx SessionContext) error) 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) Transaction added in v0.1.2

func (db *Database) Transaction(ctx context.Context, tx func(sessCtx SessionContext) error) error

Transaction Only supports single db

func (*Database) UpsertCollectionIndexMany

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

type Groups

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

Groups Mongodb instance groups

func NewGroups

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

NewGroups mongo instance groups

func (*Groups) Disconnect

func (g *Groups) Disconnect() error

func (*Groups) GetInstance

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

GetInstance get instance by database

type HostInfoResp added in v0.1.1

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

type Index

type Index struct {
	Collection         string
	Name               string // index name
	Keys               bson.D
	Unique             bool  // unique index
	Background         bool  // background create index
	ExpireAfterSeconds int32 // ttl index
}

func (Index) Validate

func (i Index) Validate() error

type SessionContext added in v0.1.2

type SessionContext mongo.SessionContext

type ShardCollection

type ShardCollection struct {
	Prefix string
	Sep    string // sep tag
	// contains filtered or unexported fields
}

ShardCollection sharding collection name

func NewShardCollection

func NewShardCollection(prefix string, day int) ShardCollection

prefix collection name prefix day>=31 | day<=0 month, day = x, span = x

func (ShardCollection) CollNameByStartEnd

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

CollNameByStartEnd find collection names by bucket,start,end

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 calc next sep time

Jump to

Keyboard shortcuts

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