nosql

package module
v0.0.0-...-d25e411 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultMongoClient    = "default-mongo"
	DefaultConnectTimeout = 3 * time.Second
)

Variables

View Source
var (
	MongoStdLogger stdLogger
)

Functions

func InitMongoClient

func InitMongoClient(clientName, username, password string, addrs []string, mongoPoolLimit uint64) error

Types

type CursorCallBackFunc

type CursorCallBackFunc func(res interface{}, err error)

type MgClient

type MgClient struct {
	*mongo.Client
}

func GetMongoClient

func GetMongoClient(clientName string) *MgClient

func (*MgClient) AggregateUseCursor

func (client *MgClient) AggregateUseCursor(db string, table string, queries []bson.D, rowType interface{}, cursorCallbackFunc CursorCallBackFunc) error

func (*MgClient) Close

func (client *MgClient) Close()

func (*MgClient) CopyTable

func (client *MgClient) CopyTable(db, table, newTable string) (bool, error)

func (*MgClient) CreateIndex

func (client *MgClient) CreateIndex(db, table, key string, uniqueKey bool) error

CreateIndex .

func (*MgClient) CreateMultiIndex

func (client *MgClient) CreateMultiIndex(db, table string, keys []string, uniqueKey bool) error

创建多个索引

func (*MgClient) DeleteMany

func (client *MgClient) DeleteMany(db string, table string, filter bson.D) error

func (*MgClient) DeleteOne

func (client *MgClient) DeleteOne(db string, table string, filter bson.D) error

func (*MgClient) Distinct

func (client *MgClient) Distinct(db, table string, filter bson.D, distinctField string) (result []interface{}, err error)

func (*MgClient) Drop

func (client *MgClient) Drop(db, table string) error

func (*MgClient) EstimatedDocumentCount

func (client *MgClient) EstimatedDocumentCount(db, table string) (int64, error)

通过metadata获取整个集合中总记录数

func (*MgClient) Find

func (client *MgClient) Find(db string, table string, filter interface{}, result interface{}) (bool, error)

func (*MgClient) FindByID

func (client *MgClient) FindByID(db, table string, id interface{}, resultObj interface{}) error

func (*MgClient) FindOne

func (client *MgClient) FindOne(db, table string, filter bson.D, resultObj interface{}) error

FindOne 查询一条数据 query example bson.D{{"name", 1}, {"age", 1}}

func (*MgClient) FindUseCursor

func (client *MgClient) FindUseCursor(db string, table string, batchSize int32, filter bson.D, rowType interface{}, cursorCallbackFunc CursorCallBackFunc) error

func (*MgClient) FindUseCursorWithOptions

func (client *MgClient) FindUseCursorWithOptions(db string, table string, batchSize int32, filter bson.D, rowType interface{}, opts *options.FindOptions, cursorCallbackFunc CursorCallBackFunc) error

func (*MgClient) FindWithOpts

func (client *MgClient) FindWithOpts(db string, table string, offset, limit int64, filter interface{}, opts *options.FindOptions, result interface{}) (bool, error)

func (*MgClient) FindWithOrder

func (client *MgClient) FindWithOrder(db string, table string, filter bson.D, orders map[string]int, result interface{}) (bool, error)

func (*MgClient) InsertMany

func (client *MgClient) InsertMany(db string, table string, docs ...interface{}) error

example InsertMany("db","table",bson.D{{"name", "Alice"}},bson.D{{"name", "Bob"}})

func (*MgClient) InsertManyTryBest

func (client *MgClient) InsertManyTryBest(db string, table string, docs ...interface{}) error

上面的InsertMany在遇到异常的时候(比如插入mongo集群中已存在的数据),全部文档都会插入失败 这个方法则忽略异常的文档,将没出问题的这部分文档写入到mongo 检测插入过程的错误可以使用下面的方式 err := GetMongoClient(DefaultMongoClient).InsertManyTryBest("db", "table", doc) we, ok := err.(mongo.BulkWriteException) if ok { TO DO ... } 出现重复文档的code = 11000 if we.HasErrorCode(11000) { TO DO ... }

func (*MgClient) QueryCount

func (client *MgClient) QueryCount(db, table string, filter bson.D, defaultVal int) (int64, error)

func (*MgClient) RenameTable

func (client *MgClient) RenameTable(db, table, newTable string) error

func (*MgClient) ReplaceOne

func (client *MgClient) ReplaceOne(db string, table string, filter interface{}, doc interface{}) error

func (*MgClient) UpdateMany

func (client *MgClient) UpdateMany(db string, table string, filter interface{}, update interface{}) error

example filter := bson.D{{"birthday", today}} update := bson.D{{"$inc", bson.D{{"age", 1}}}}

func (*MgClient) UpdateOne

func (client *MgClient) UpdateOne(db string, table string, filter interface{}, update interface{}) error

example filter := bson.D{{"_id", id}}

update := bson.D{{"email", "newemail@example.com"}}

func (*MgClient) Upsert

func (client *MgClient) Upsert(db string, table string, filter interface{}, doc interface{}) error

Upsert doc是bson格式

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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