mongo

package
v0.0.19 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	EqualPredicate              = Predicate("$eq")
	NotEqualPredicate           = Predicate("$ne")
	GreaterThanPredicate        = Predicate("$gt")
	GreaterThanOrEqualPredicate = Predicate("$gte")
	SmallerThanPredicate        = Predicate("$lt")
	SmallerThanOrEqualPredicate = Predicate("$lte")
	InPredicate                 = Predicate("$in")
	NotInPredicate              = Predicate("$nin")
	AndPredicate                = Predicate("$and")
	OrPredicate                 = Predicate("$or")
	NotPredicate                = Predicate("$not")
	NorPredicate                = Predicate("$nor")
	RegexPredicate              = Predicate("$regex")
	OptionsPredicate            = Predicate("$options") // i

)
View Source
var EmptyObjectId primitive.ObjectID

Functions

func BsonID

func BsonID(id string) primitive.ObjectID

func ConvertIdArrayToString

func ConvertIdArrayToString(list []primitive.ObjectID) string

func ConvertIdSliceToStringSlice

func ConvertIdSliceToStringSlice(list []primitive.ObjectID) []string

func ConvertIdStringToArray

func ConvertIdStringToArray(idStr *string) []primitive.ObjectID

func ConvertStructToBsonM

func ConvertStructToBsonM(s any) *bson.M

old function, replaced by StructToMongoDoc()

func GetBsonM added in v0.0.10

func GetBsonM[T interface{}](mb *model.ModelBuilder[T]) bson.M

for mongo

func StructToMongoDoc

func StructToMongoDoc(s any) *bson.M

using dot notation to specify the fields in an embedded document for partial update

{
	"details": {"a": 1, "b": 2}
}

convert to:

{
	"details.a": 1,
	"details.b": 2
}

func UnmarshalBsonMToStruct

func UnmarshalBsonMToStruct[T any](doc *bson.M) (t *T)

func UnmarshalBsonMsToStructs

func UnmarshalBsonMsToStructs[T any](docs []bson.M) []T

Types

type BaseService added in v0.0.16

type BaseService[T interface{}] struct {
	// contains filtered or unexported fields
}

func NewBaseService added in v0.0.16

func NewBaseService[T interface{}](col *Collection[T]) *BaseService[T]

func (*BaseService[T]) All added in v0.0.16

func (s *BaseService[T]) All(ctx core.Context) (data []T, err error)

func (*BaseService[T]) ById added in v0.0.16

func (s *BaseService[T]) ById(ctx core.Context, id string) (data *T, err error)

func (*BaseService[T]) ByIds added in v0.0.17

func (s *BaseService[T]) ByIds(ctx core.Context, id []string) (data []T, err error)

func (*BaseService[T]) Col added in v0.0.16

func (s *BaseService[T]) Col() *Collection[T]

func (*BaseService[T]) DeleteById added in v0.0.16

func (s *BaseService[T]) DeleteById(ctx core.Context, id string) (err error)

func (*BaseService[T]) NewQueryBuilder added in v0.0.16

func (s *BaseService[T]) NewQueryBuilder() *ModelQueryBuilder[T]

func (*BaseService[T]) Total added in v0.0.16

func (s *BaseService[T]) Total(ctx core.Context) (int64, error)

type Collection

type Collection[T interface{}] struct {
	mongo.Collection
}

func NewColl

func NewColl[T interface{}](db MongoDB, optFuncs ...OptFunc) (c *Collection[T])

func (*Collection[T]) Aggregate

func (c *Collection[T]) Aggregate(filter interface{}, opts ...*options.AggregateOptions) (all []interface{})

func (*Collection[T]) Count

func (c *Collection[T]) Count(filter interface{}, opts ...*options.CountOptions) (int64, error)

func (*Collection[T]) Create

func (c *Collection[T]) Create(doc *T) error

func (*Collection[T]) DB

func (c *Collection[T]) DB() *mongo.Database

func (*Collection[T]) DeleteAllByFilter

func (c *Collection[T]) DeleteAllByFilter(filter interface{}) (err error)

func (*Collection[T]) DeleteById

func (c *Collection[T]) DeleteById(id primitive.ObjectID) error

func (*Collection[T]) DeleteByIdStr added in v0.0.9

func (c *Collection[T]) DeleteByIdStr(id *string) error

func (*Collection[T]) DeleteOneByFilter

func (c *Collection[T]) DeleteOneByFilter(filter interface{}) (err error)

func (*Collection[T]) Distinct

func (c *Collection[T]) Distinct(field string, filter interface{}, opts ...*options.DistinctOptions) (all []interface{}, e error)

func (*Collection[T]) DropCollection

func (c *Collection[T]) DropCollection()

func (*Collection[T]) ExistById

func (c *Collection[T]) ExistById(id string) (bool, error)

func (*Collection[T]) FindAll

func (c *Collection[T]) FindAll(opts ...*options.FindOptions) (all []T, err error)

func (*Collection[T]) FindAllByFilter

func (c *Collection[T]) FindAllByFilter(filter interface{}, opts ...*options.FindOptions) (all []T, err error)

func (*Collection[T]) FindAllByIdStrList added in v0.0.17

func (c *Collection[T]) FindAllByIdStrList(ids []string, opts ...*options.FindOptions) ([]T, error)

func (*Collection[T]) FindAllPagedByFilter

func (c *Collection[T]) FindAllPagedByFilter(filter interface{}, page int64, pageSize int64) ([]T, error)

page start from 0

func (*Collection[T]) FindOneByFilter

func (c *Collection[T]) FindOneByFilter(filter interface{}, opts ...*options.FindOneOptions) (*T, error)

func (*Collection[T]) FindOneById

func (c *Collection[T]) FindOneById(id primitive.ObjectID, opts ...*options.FindOneOptions) (*T, error)

func (*Collection[T]) FindOneByIdStr

func (c *Collection[T]) FindOneByIdStr(id string, opts ...*options.FindOneOptions) (*T, error)

func (*Collection[T]) GetIndexes

func (c *Collection[T]) GetIndexes() (indexes map[string]any)

func (*Collection[T]) InsertMany

func (c *Collection[T]) InsertMany(doc []interface{}) (err error)

func (*Collection[T]) InsertOne

func (c *Collection[T]) InsertOne(doc *T) (err error)

insert one with full doc, so we can use T, BUT UPDATE IS PARTIAL

func (*Collection[T]) InsertOneGeneral

func (c *Collection[T]) InsertOneGeneral(doc interface{}) (err error)

insert one with full doc, so we can use T, BUT UPDATE IS PARTIAL

func (*Collection[T]) SetIndexes

func (c *Collection[T]) SetIndexes(key string, unique bool) error

func (*Collection[T]) Total

func (c *Collection[T]) Total(opts ...*options.CountOptions) (int64, error)

func (*Collection[T]) UpdateOne

func (c *Collection[T]) UpdateOne(filter interface{}, update interface{}) (err error)

func (*Collection[T]) UpdateOneByFilter

func (c *Collection[T]) UpdateOneByFilter(filter *bson.M, doc interface{}) error

func (*Collection[T]) UpdateOneById

func (c *Collection[T]) UpdateOneById(objId primitive.ObjectID, doc interface{}) error

func (*Collection[T]) UpdateOneByIdStr

func (c *Collection[T]) UpdateOneByIdStr(objId *string, doc interface{}) error

type ModelQueryBuilder

type ModelQueryBuilder[T interface{}] struct {
	// contains filtered or unexported fields
}

func (*ModelQueryBuilder[T]) AddOrder

func (qb *ModelQueryBuilder[T]) AddOrder(attr string, asc bool) *ModelQueryBuilder[T]

func (*ModelQueryBuilder[T]) AddOrder2

func (qb *ModelQueryBuilder[T]) AddOrder2(attr string, order model.OrderType) *ModelQueryBuilder[T]

func (*ModelQueryBuilder[T]) AddOrderByOpt

func (qb *ModelQueryBuilder[T]) AddOrderByOpt(w *OrderOption) *ModelQueryBuilder[T]

func (*ModelQueryBuilder[T]) AddWhere

func (qb *ModelQueryBuilder[T]) AddWhere(attr string, value interface{}, predicate Predicate) *ModelQueryBuilder[T]

func (*ModelQueryBuilder[T]) AddWhereAnd

func (qb *ModelQueryBuilder[T]) AddWhereAnd(value interface{}) *ModelQueryBuilder[T]

func (*ModelQueryBuilder[T]) AddWhereByOpt

func (qb *ModelQueryBuilder[T]) AddWhereByOpt(w *WhereOption) *ModelQueryBuilder[T]

func (*ModelQueryBuilder[T]) AddWhereLogic

func (qb *ModelQueryBuilder[T]) AddWhereLogic(value interface{}, logicPredicate Predicate) *ModelQueryBuilder[T]

or/and logic

func (*ModelQueryBuilder[T]) AddWhereOr

func (qb *ModelQueryBuilder[T]) AddWhereOr(value interface{}) *ModelQueryBuilder[T]

func (*ModelQueryBuilder[T]) BuildQuery

func (qb *ModelQueryBuilder[T]) BuildQuery() *bson.M

func (*ModelQueryBuilder[T]) ClearOrders

func (qb *ModelQueryBuilder[T]) ClearOrders() *ModelQueryBuilder[T]

func (*ModelQueryBuilder[T]) ClearWheres

func (qb *ModelQueryBuilder[T]) ClearWheres() *ModelQueryBuilder[T]

func (*ModelQueryBuilder[T]) Count

func (qb *ModelQueryBuilder[T]) Count(col MongoCollInterface[T]) (int64, error)

func (*ModelQueryBuilder[T]) Delete

func (qb *ModelQueryBuilder[T]) Delete(col MongoCollInterface[T]) (err error)

func (*ModelQueryBuilder[T]) First

func (qb *ModelQueryBuilder[T]) First(col MongoCollInterface[T]) (*T, error)

func (*ModelQueryBuilder[T]) Limit

func (qb *ModelQueryBuilder[T]) Limit(limit int64) *ModelQueryBuilder[T]

func (*ModelQueryBuilder[T]) Offset

func (qb *ModelQueryBuilder[T]) Offset(offset int64) *ModelQueryBuilder[T]

func (*ModelQueryBuilder[T]) OrderById

func (qb *ModelQueryBuilder[T]) OrderById(asc bool) *ModelQueryBuilder[T]

func (*ModelQueryBuilder[T]) QueryAll

func (qb *ModelQueryBuilder[T]) QueryAll(col MongoCollInterface[T]) ([]T, error)

func (*ModelQueryBuilder[T]) QueryOne

func (qb *ModelQueryBuilder[T]) QueryOne(col MongoCollInterface[T]) (*T, error)

func (*ModelQueryBuilder[T]) Updates

func (qb *ModelQueryBuilder[T]) Updates(col MongoCollInterface[T], m map[string]interface{}) (err error)

func (*ModelQueryBuilder[T]) WhereId

func (qb *ModelQueryBuilder[T]) WhereId(p Predicate, value string) *ModelQueryBuilder[T]

func (*ModelQueryBuilder[T]) WhereIdIn

func (qb *ModelQueryBuilder[T]) WhereIdIn(value []string) *ModelQueryBuilder[T]

type MongoCollInterface

type MongoCollInterface[T interface{}] interface {
	UpdateOneByFilter(filter *bson.M, doc interface{}) error
	DeleteAllByFilter(filter interface{}) (err error)
	FindOneByFilter(filter interface{}, opts ...*options.FindOneOptions) (*T, error)
	FindAllByFilter(filter interface{}, opts ...*options.FindOptions) (all []T, err error)

	Count(filter interface{}, opts ...*options.CountOptions) (int64, error)
}

type MongoConfig

type MongoConfig struct {
	Coll             string   `json:"coll" yaml:"coll" mapstructure:"coll"`                                           // collection name
	Options          string   `json:"options" yaml:"options" mapstructure:"options"`                                  // mongodb options
	Database         string   `json:"database" yaml:"database" mapstructure:"database"`                               // database name
	Username         string   `json:"username" yaml:"username" mapstructure:"username"`                               // 用户名
	Password         string   `json:"password" yaml:"password" mapstructure:"password"`                               // 密码
	AuthSource       string   `json:"auth-source" yaml:"auth-source" mapstructure:"auth-source"`                      // 验证数据库
	MinPoolSize      uint64   `json:"min-pool-size" yaml:"min-pool-size" mapstructure:"min-pool-size"`                // 最小连接池
	MaxPoolSize      uint64   `json:"max-pool-size" yaml:"max-pool-size" mapstructure:"max-pool-size"`                // 最大连接池
	SocketTimeoutMs  int64    `json:"socket-timeout-ms" yaml:"socket-timeout-ms" mapstructure:"socket-timeout-ms"`    // socket超时时间
	ConnectTimeoutMs int64    `json:"connect-timeout-ms" yaml:"connect-timeout-ms" mapstructure:"connect-timeout-ms"` // 连接超时时间
	IsZap            bool     `json:"is-zap" yaml:"is-zap" mapstructure:"is-zap"`                                     // 是否开启zap日志
	Hosts            []string `json:"hosts" yaml:"hosts" mapstructure:"hosts"`                                        // 主机列表, host format: ip:port
	CaFile           string   `json:"ca-file" yaml:"ca-file" mapstructure:"ca-file"`                                  // ca file path
}

func (*MongoConfig) JoinHosts

func (x *MongoConfig) JoinHosts() string

func (*MongoConfig) Uri

func (x *MongoConfig) Uri() string

type MongoDB

type MongoDB interface {
	GetDb() *mongo.Database
	GetClient() *mongo.Client
	DbClose() error

	RunCommand(cmd interface{}) (*bson.M, error)
	// contains filtered or unexported methods
}

func NewDB

func NewDB(config *MongoConfig) (MongoDB, error)

type OptFunc

type OptFunc func(opt *collOpts)

func SetColl

func SetColl(collName string) OptFunc

type OrderOption

type OrderOption struct {
	Attr  string
	Order model.OrderType
}

type Predicate

type Predicate string

type WhereOption

type WhereOption struct {
	Attr      string
	Predicate Predicate
	Value     interface{}
}

Directories

Path Synopsis
/////////////////////////////////////// not used yet ///////////////////////////////////////
/////////////////////////////////////// not used yet ///////////////////////////////////////

Jump to

Keyboard shortcuts

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