zmgo

package
v3.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2022 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MongoConnPoolLimit is the maximum number of sockets in use in a single server
	// before this session will block waiting for a socket to be available.
	// The default limit is 4096.
	//
	// This limit must be set to cover more than any expected workload of the
	// application. It is a bad practice and an unsupported use case to use the
	// database driver to define the concurrency limit of an application. Prevent
	// such concurrency "at the door" instead, by properly restricting the amount
	// of used resources and number of goroutines before they are created.
	MongoConnPoolLimit = 128
)

Variables

View Source
var (
	ClientMap    map[string]*MongoClient
	CommonClient *MongoClient
	IsInit       bool
)

Functions

func Aggregate

func Aggregate(result interface{}, dbName, collName string, pipeline interface{}, opts ...*options.AggregateOptions) error

func BulkWrite

func BulkWrite(dbName, collName string, models []mongo.WriteModel, opts ...*options.BulkWriteOptions) error

func Collection

func Collection(projectId, collection string) *mongo.Collection

func Count

func Count(dbName, collName string, filter interface{}, opts ...*options.CountOptions) (int64, error)

func DB

func DB(projectId string, dbname string) *mongo.Database

func DecodeAll

func DecodeAll(cursor *mongo.Cursor, ctx context.Context, result interface{}) (err error)

DecodeAll 反射读取所有数据

func DeleteMany

func DeleteMany(dbName, collName string, filter interface{}, opts ...*options.DeleteOptions) error

func DeleteOne

func DeleteOne(dbName, collName string, filter interface{}, opts ...*options.DeleteOptions) error

func EnsureExpireIndex

func EnsureExpireIndex(db *mongo.Database, collection string, expireTime int32, keys ...string)

func EnsureIndex

func EnsureIndex(db *mongo.Database, collection string, keys ...string)

func EnsureUniqIndex

func EnsureUniqIndex(db *mongo.Database, collection string, keys ...string)

func FindAll

func FindAll(result interface{}, dbName, collName string, query interface{}, opts ...*options.FindOptions) error

func FindOne

func FindOne(result interface{}, proj string, collName string, query interface{}, opts ...*options.FindOneOptions) error

func FindOneAndDelete

func FindOneAndDelete(dbName, collName string, filter interface{}, opts ...*options.FindOneAndDeleteOptions) error

func FindOneAndUpdate

func FindOneAndUpdate(dbName, collName string, filter interface{}, update interface{}, opts ...*options.FindOneAndUpdateOptions) error

func GetCustomTLSConfig

func GetCustomTLSConfig(caFile string) (*tls.Config, error)

GetCustomTLSConfig 获取TLS证书

func Init

func Init(adds map[string]string, prom bool)

Init ClientMap and CommonClient

func InsertMany

func InsertMany(dbName, collName string, documents []interface{}, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error)

func InsertOne

func InsertOne(dbName, collName string, document interface{}, opts ...*options.InsertOneOptions) (interface{}, error)

func NewInt64

func NewInt64(value int64) *int64

func NewString

func NewString(value string) *string

func NewTrue

func NewTrue() *bool

func SetFindClient

func SetFindClient(Func GetClientFunc)

SetFindClient If you want select client by yourself you can do it

func UpdateAll

func UpdateAll(dbName, collName string, filter interface{}, update interface{}, opts ...*options.UpdateOptions) error

func UpdateOne

func UpdateOne(dbName, collName string, filter interface{}, update interface{}, opts ...*options.UpdateOptions) error

func UpsertOne

func UpsertOne(dbName, collName string, filter interface{}, update interface{}, opts ...*options.UpdateOptions) error

Types

type GetClientFunc

type GetClientFunc func(projectId string) (*MongoClient, error)

GetClientFunc You can select your client by yourself

var GetClient GetClientFunc = func(projectId string) (client *MongoClient, err error) {
	if !IsInit {
		err = errors.New("Mongo Client is not init!")
		return
	}

	if c, ok := ClientMap[projectId]; ok {
		client = c
	} else {
		client = CommonClient
	}
	return
}

GetClient default GetClient func is get client from ClientMap

type MongoClient

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

func NewClient

func NewClient(prom bool, opts ...*options.ClientOptions) (*MongoClient, error)

NewClient NewMongoClient create MongoClient by your options.ClientOptions

func NewDocumentClient

func NewDocumentClient(uri, CAFile string) (*MongoClient, error)

NewDocumentClient create MongoClient use default options.ClientOptions and TLS

func NewDocumentClientWithProm

func NewDocumentClientWithProm(uri, CAFile string) (*MongoClient, error)

NewDocumentClientWithProm create MongoClient use Prom Monitor

func NewMongoClient

func NewMongoClient(uri string) (*MongoClient, error)

NewMongoClient create MongoClient use default options.ClientOptions

func NewMongoClientWithProm

func NewMongoClientWithProm(uri string, prom bool) (*MongoClient, error)

NewMongoClientWithProm create MongoClient use Prom Monitor

func (*MongoClient) Aggregate

func (c *MongoClient) Aggregate(result interface{}, dbName, collName string, pipeline interface{}, opts ...*options.AggregateOptions) (err error)

func (*MongoClient) BulkWrite

func (c *MongoClient) BulkWrite(dbName, collName string, models []mongo.WriteModel, opts ...*options.BulkWriteOptions) (err error)

func (*MongoClient) Count

func (c *MongoClient) Count(dbName, collName string, filter interface{}, opts ...*options.CountOptions) (count int64, err error)

func (*MongoClient) Database

func (c *MongoClient) Database(name string) *mongo.Database

func (*MongoClient) DbColl

func (c *MongoClient) DbColl(dbName, coll string) *mongo.Collection

func (*MongoClient) DeleteMany

func (c *MongoClient) DeleteMany(dbName, collName string, filter interface{}, opts ...*options.DeleteOptions) (err error)

func (*MongoClient) DeleteOne

func (c *MongoClient) DeleteOne(dbName, collName string, filter interface{}, opts ...*options.DeleteOptions) (err error)

func (*MongoClient) FindAll

func (c *MongoClient) FindAll(result interface{}, dbName, collName string, query interface{}, opts ...*options.FindOptions) (err error)

func (*MongoClient) FindOne

func (c *MongoClient) FindOne(result interface{}, dbName, collName string, query interface{}, opts ...*options.FindOneOptions) (err error)

func (*MongoClient) FindOneAndDelete

func (c *MongoClient) FindOneAndDelete(dbName, collName string, filter interface{}, opts ...*options.FindOneAndDeleteOptions) (err error)

func (*MongoClient) FindOneAndUpdate

func (c *MongoClient) FindOneAndUpdate(dbName, collName string, filter interface{}, update interface{}, opts ...*options.FindOneAndUpdateOptions) (err error)

func (*MongoClient) InsertMany

func (c *MongoClient) InsertMany(dbName, collName string, documents []interface{}, opts ...*options.InsertManyOptions) (result *mongo.InsertManyResult, err error)

func (*MongoClient) InsertOne

func (c *MongoClient) InsertOne(dbName, collName string, document interface{}, opts ...*options.InsertOneOptions) (insertedID interface{}, err error)

func (*MongoClient) UpdateAll

func (c *MongoClient) UpdateAll(dbName, collName string, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (err error)

func (*MongoClient) UpdateOne

func (c *MongoClient) UpdateOne(dbName, collName string, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (err error)

func (*MongoClient) UpsertOne

func (c *MongoClient) UpsertOne(dbName, collName string, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (err error)

type MyCursor

type MyCursor struct {
	*mongo.Cursor
}

func NewMyCursor

func NewMyCursor(c *mongo.Cursor) *MyCursor

func (*MyCursor) All

func (mc *MyCursor) All(result interface{}) (err error)

Jump to

Keyboard shortcuts

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