mongox

package
v1.0.0-alpha Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2023 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CloneError = errors.New("dummyCollection not support clone")

Functions

func NewCustomRegistry

func NewCustomRegistry() *bsoncodec.Registry

NewCustomRegistry create custom registry

Types

type Collection

type Collection interface {
	Clone(opts ...*options.CollectionOptions) (*mongo.Collection, error)
	Name() string
	Database() *mongo.Database
	BulkWrite(ctx context.Context, models []mongo.WriteModel,
		opts ...*options.BulkWriteOptions) (*mongo.BulkWriteResult, error)
	// InsertOne 插入一条
	InsertOne(ctx context.Context, document interface{}, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error)
	// InsertMany 插入多条
	InsertMany(ctx context.Context, documents []interface{},
		opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error)
	// DeleteOne 删除一条
	DeleteOne(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
	// DeleteMany 删除多条
	DeleteMany(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
	// UpdateOne 更新一条
	UpdateOne(ctx context.Context, filter interface{}, update interface{},
		opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
	// UpdateMany 更新多条
	UpdateMany(ctx context.Context, filter interface{}, update interface{},
		opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
	// ReplaceOne 替换一条
	ReplaceOne(ctx context.Context, filter interface{}, replacement interface{},
		opts ...*options.ReplaceOptions) (*mongo.UpdateResult, error)
	// Aggregate 管道聚合操作
	Aggregate(ctx context.Context, pipeline interface{}, opts ...*options.AggregateOptions) (*mongo.Cursor, error)
	// CountDocuments 统计文档数量
	CountDocuments(ctx context.Context, filter interface{}, opts ...*options.CountOptions) (int64, error)
	EstimatedDocumentCount(ctx context.Context, opts ...*options.EstimatedDocumentCountOptions) (int64, error)
	Distinct(ctx context.Context, fieldName string, filter interface{},
		opts ...*options.DistinctOptions) ([]interface{}, error)
	// Find 自定义查询
	Find(ctx context.Context, filter interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error)

	// FindPage 分页查询
	FindPage(ctx context.Context, filter interface{}, param *page.Params) (page.Page, error)
	// FindOne 查询一条
	FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) *mongo.SingleResult
	// FindOneAndDelete 查询一条并删除
	FindOneAndDelete(ctx context.Context, filter interface{}, opts ...*options.FindOneAndDeleteOptions) *mongo.SingleResult
	// FindOneAndReplace 查询一条并替换
	FindOneAndReplace(ctx context.Context, filter interface{}, replacement interface{},
		opts ...*options.FindOneAndReplaceOptions) *mongo.SingleResult
	// FindOneAndUpdate 查询一条并更新
	FindOneAndUpdate(ctx context.Context, filter interface{}, update interface{},
		opts ...*options.FindOneAndUpdateOptions) *mongo.SingleResult
	Watch(ctx context.Context, pipeline interface{}, opts ...*options.ChangeStreamOptions) (*mongo.ChangeStream, error)
	Indexes() mongo.IndexView
	Drop(ctx context.Context) error
}

Collection is a handle to a MongoDB collection. It is safe for concurrent use by multiple goroutines.

type DataBase

type DataBase interface {
	// Name returns the name of the database.
	Name() string

	// Collection 获取文档集合
	Collection(name string, opts ...*options.CollectionOptions) Collection
}

func NewDataBase

func NewDataBase(cli *mongo.Client, dbname string) DataBase

type Info

type Info struct {
	Addrs    []string `toml:"Addrs" json:"Addrs"`
	Database string   `toml:"Database" json:"Database"`
	Username string   `toml:"Username" json:"Username"`
	Password string   `toml:"pwd" json:"pwd"`
	MaxConn  int      `toml:"MaxConn" json:"MaxConn"`
	TimeOut  int      `toml:"TimeOut" json:"TimeOut"` // 超时时间, 单位秒
}

type Mongo

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

func (*Mongo) Close

func (c *Mongo) Close()

Close mongo client

func (*Mongo) DefaultDatabase

func (c *Mongo) DefaultDatabase() DataBase

func (*Mongo) GetDatabase

func (c *Mongo) GetDatabase(name string) DataBase

func (*Mongo) InitDefaultDatabase

func (c *Mongo) InitDefaultDatabase(name string) error

type Option

type Option func(*option)

func WithTrace

func WithTrace(t Trace) Option

WithTrace 设置trace信息

type Repo

type Repo interface {
	Close()
	// GetDatabase 初始化一个新的 mongo-database
	GetDatabase(name string) DataBase

	// InitDefaultDatabase 初始化 当前应用默认 mongo-database
	InitDefaultDatabase(name string) error

	// DefaultDatabase 获取当前应用 默认 mongo-database
	DefaultDatabase() DataBase
}

func Default

func Default() Repo

func New

func New(cfg Info) (Repo, error)

type Trace

type Trace = trace.T

Jump to

Keyboard shortcuts

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