XMongo

package
v1.2.8 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2021 License: MIT Imports: 6 Imported by: 0

README

XMongo Starter

基于 go.mongodb.org/mongo-driver 官方包

Mongodb Documentation

Documentation https://docs.mongodb.com/drivers/go

Example https://github.com/simagix/mongo-go-examples

XMongo Starter Usage
goinfras.RegisterStarter(XMongo.NewStarter())

XMongo Config Setting
DbHosts               []string
DbUser                string
DbPasswd              string
Database              string
ReplicaSet            string   // 指定群集的副本集名称。如果指定,集群将被视为副本集,驱动程序将自动发现集中的所有服务器,从通过ApplyURI或SetHosts指定的节点开始。副本集中的所有节点必须具有相同的副本集名称,否则客户端不会将它们视为该集的一部分。
PasswordSet           bool     // 对于GSSAPI,如果指定了密码,则此值必须为true,即使密码是空字符串,并且 如果未指定密码,则为false,表示应从运行的上下文中获取密码 过程。对于其他机制,此字段将被忽略。
LocalThreshold        int      // 指定“延迟窗口”的宽度:在为一个操作选择多个合适的服务器时,这是最短和最长平均往返时间之间可接受的非负增量。延迟窗口中的服务器是随机选择的。默认值为15毫秒。
Compressors           []string // 通信数据压缩器,可多选
Direct                bool     // 是否单机直连
HeartbeatInterval     int      // 定期连接心跳检查,不设置默认10s
MinPoolSize           uint64   // 最小连接池连接数
MaxPoolSize           uint64   // 最大连接池连接数
MaxConnIdleTime       uint64   // 连接池闲置连结束最大保持时间,0时表示无限制保持闲置连接状态
AutoEncryptionOptions bool     // 作用于collection的自动加密
ConnectTimeout        int      // 接超时时间,单位秒
RetryReads            bool     // 指定是否应在某些错误(如网络)上重试一次受支持的读操作
RetryWrites           bool     // 指定是否应在某些错误(如网络)上重试一次受支持的写入操作
XMongo Usage

// 通用操作:增删改查
commonMongoDao := XMongo.XCommon("dev_test")
// 增
insertID, err := commonMongoDao.InsertOne(context.TODO(), "demo", bson.M{"name": "joker"})
So(err, ShouldBeNil)
Println("InsertID", insertID)

// 查
result := commonMongoDao.FindOne(context.TODO(), "demo", bson.M{"name": "joker"})
So(result.Err(), ShouldBeNil)
res := bson.M{}
err = result.Decode(res)
So(err, ShouldBeNil)
Println("FindOne Result", res)

// 改
opts := options.Update().SetUpsert(true)
filter := bson.D{{"_id", insertID}}
update := bson.D{{"$set", bson.D{{"name", "ken"}}}}
updateResult, err := commonMongoDao.UpdateOne(context.TODO(), "demo", filter, update, opts)
So(err, ShouldBeNil)
Println("Update Result", updateResult)

// 删
deleteCount, err := commonMongoDao.DeleteOne(context.TODO(), "demo", bson.D{{"_id", insertID}})
So(err, ShouldBeNil)
Println("Delete Count", deleteCount)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateDefaultDB

func CreateDefaultDB(config *Config) error

创建一个默认配置的Manager

func NewClient

func NewClient(cfg *Config) (mc *mongo.Client, err error)

func NewStarter

func NewStarter() *starter

func XClient

func XClient() *mongo.Client

func XFClient

func XFClient(f func(c *mongo.Client) error) error

资源组件闭包执行

Types

type CommonMongoDao

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

func XCommon

func XCommon(dbName string) *CommonMongoDao

mongodb 通用操作实例

func (*CommonMongoDao) Aggregate

func (mp *CommonMongoDao) Aggregate(ctx context.Context, collectionName string, pipline interface{}, opts ...*options.AggregateOptions) (cursor *mongo.Cursor, err error)

聚合计算

func (*CommonMongoDao) BulkWrite

func (mp *CommonMongoDao) BulkWrite(ctx context.Context, collectionName string, models []mongo.WriteModel, opts ...*options.BulkWriteOptions) (bulkResult *mongo.BulkWriteResult, err error)

批量操作

func (*CommonMongoDao) CountDocuments

func (mp *CommonMongoDao) CountDocuments(ctx context.Context, collectionName string, filter interface{}, opts ...*options.CountOptions) (int64, error)

查找文档数

func (*CommonMongoDao) DM

func (mp *CommonMongoDao) DM(dbName, colName string, f func(c *mongo.Collection) error) error

func (*CommonMongoDao) DeleteMany

func (mp *CommonMongoDao) DeleteMany(ctx context.Context, collectionName string, filter interface{}, opts ...*options.DeleteOptions) (deleteCount int64, err error)

func (*CommonMongoDao) DeleteOne

func (mp *CommonMongoDao) DeleteOne(ctx context.Context, collectionName string, filter interface{}, opts ...*options.DeleteOptions) (deleteCount int64, err error)

通用删除数据

func (*CommonMongoDao) Find

func (mp *CommonMongoDao) Find(ctx context.Context, collectionName string, filter interface{}, opts ...*options.FindOptions) (cursor *mongo.Cursor, err error)

查找文档

func (*CommonMongoDao) FindOne

func (mp *CommonMongoDao) FindOne(ctx context.Context, collectionName string, filter interface{}, opts ...*options.FindOneOptions) *mongo.SingleResult

func (*CommonMongoDao) FindOneAndDelete

func (mp *CommonMongoDao) FindOneAndDelete(ctx context.Context, collectionName string, filter interface{}, opts ...*options.FindOneAndDeleteOptions) (singleResult *mongo.SingleResult)

func (*CommonMongoDao) FindOneAndReplace

func (mp *CommonMongoDao) FindOneAndReplace(ctx context.Context, collectionName string, filter interface{}, replacement interface{}, opts ...*options.FindOneAndReplaceOptions) (singleResult *mongo.SingleResult)

func (*CommonMongoDao) FindOneAndUpdate

func (mp *CommonMongoDao) FindOneAndUpdate(ctx context.Context, collectionName string, filter, updater interface{}, opts ...*options.FindOneAndUpdateOptions) (result *mongo.SingleResult)

func (*CommonMongoDao) InsertMany

func (mp *CommonMongoDao) InsertMany(ctx context.Context, collectionName string, documents []interface{}, opts ...*options.InsertManyOptions) (insertIDs []interface{}, err error)

func (*CommonMongoDao) InsertOne

func (mp *CommonMongoDao) InsertOne(ctx context.Context, collectionName string, document interface{}, opts ...*options.InsertOneOptions) (insertID interface{}, err error)

通用新增数据到mongo collection

func (*CommonMongoDao) M

func (mp *CommonMongoDao) M(colName string, f func(c *mongo.Collection) error) error

func (*CommonMongoDao) ReplaceOne

func (mp *CommonMongoDao) ReplaceOne(ctx context.Context, collectionName string, filter, replacement interface{}, opts ...*options.ReplaceOptions) (result *mongo.UpdateResult, err error)

func (*CommonMongoDao) UpdateMany

func (mp *CommonMongoDao) UpdateMany(ctx context.Context, collectionName string, filter, updater interface{}, opts ...*options.UpdateOptions) (result *mongo.UpdateResult, err error)

func (*CommonMongoDao) UpdateOne

func (mp *CommonMongoDao) UpdateOne(ctx context.Context, collectionName string, filter, updater interface{}, opts ...*options.UpdateOptions) (result *mongo.UpdateResult, err error)

通用更新数据

type Config

type Config struct {
	DbHosts               []string
	DbUser                string
	DbPasswd              string
	Database              string
	ReplicaSet            string   // 指定群集的副本集名称。如果指定,集群将被视为副本集,驱动程序将自动发现集中的所有服务器,从通过ApplyURI或SetHosts指定的节点开始。副本集中的所有节点必须具有相同的副本集名称,否则客户端不会将它们视为该集的一部分。
	PasswordSet           bool     // 对于GSSAPI,如果指定了密码,则此值必须为true,即使密码是空字符串,并且 如果未指定密码,则为false,表示应从运行的上下文中获取密码 过程。对于其他机制,此字段将被忽略。
	LocalThreshold        int      // 指定“延迟窗口”的宽度:在为一个操作选择多个合适的服务器时,这是最短和最长平均往返时间之间可接受的非负增量。延迟窗口中的服务器是随机选择的。默认值为15毫秒。
	Compressors           []string // 通信数据压缩器,可多选
	Direct                bool     // 是否单机直连
	HeartbeatInterval     int      // 定期连接心跳检查,不设置默认10s
	MinPoolSize           uint64   // 最小连接池连接数
	MaxPoolSize           uint64   // 最大连接池连接数
	MaxConnIdleTime       uint64   // 连接池闲置连结束最大保持时间,0时表示无限制保持闲置连接状态
	AutoEncryptionOptions bool     // 作用于collection的自动加密
	ConnectTimeout        int      // 接超时时间,单位秒
	RetryReads            bool     // 指定是否应在某些错误(如网络)上重试一次受支持的读操作
	RetryWrites           bool     // 指定是否应在某些错误(如网络)上重试一次受支持的写入操作
}

MongoDB 配置

func DefaultConfig

func DefaultConfig() *Config

type FindResult

type FindResult interface{}

type FindResults

type FindResults []interface{}

Jump to

Keyboard shortcuts

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