mongoext

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

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

Go to latest
Published: Nov 3, 2022 License: MIT Imports: 12 Imported by: 2

README

Mongo helpers

License

Installation

go get github.com/rumorsflow/mongo-ext

License

Distributed under MIT License, please see license file within the code for more details.

Documentation

Index

Constants

View Source
const (
	QueryIndex     = "index"
	QuerySize      = "size"
	QuerySort      = "sort"
	QuerySortArr   = "sort[]"
	QueryCondition = "condition"
	QueryField     = "field"
	QueryValue     = "value"

	CondEmpty = ""
	CondEq    = "eq"
	CondNe    = "ne"
	CondGt    = "gt"
	CondGte   = "gte"
	CondLt    = "lt"
	CondLte   = "lte"
	CondIn    = "in"
	CondNin   = "nin"
	CondRegex = "regex"
	CondLike  = "like"
)
View Source
const (
	// comparison
	OpEq  = Operator("$eq")
	OpNe  = Operator("$ne")
	OpGt  = Operator("$gt")
	OpGte = Operator("$gte")
	OpLt  = Operator("$lt")
	OpLte = Operator("$lte")
	OpIn  = Operator("$in")
	OpNin = Operator("$nin")

	// logical
	OpAnd = Operator("$and")
	OpOr  = Operator("$or")
	OpNor = Operator("$nor")
	OpNot = Operator("$not")

	// array
	OpAll       = Operator("$all")
	OpSize      = Operator("$size")
	OpElemMatch = Operator("$elemMatch")

	// element
	OpExists = Operator("$exists")

	// evaluation
	OpRegex = Operator("$regex")
)
View Source
const (
	ErrMsgDecode    = "failed to decode document due to error: %w"
	ErrMsgQuery     = "failed to execute query due to error: %w"
	ErrMsgMarshal   = "failed to marshal document due to error: %w"
	ErrMsgUnmarshal = "failed to unmarshal document due to error: %w"
)
View Source
const (
	DefaultSize = int64(20)
	Timeout     = 5 * time.Second
)
View Source
const ErrMsgClient = "failed to create mongodb client due to error: %w"
View Source
const ErrMsgDatabase = "failed to create mongodb database due to error: %w"

Variables

View Source
var ErrNoDB = errors.New("database Name OpNot found in URI")

Functions

func Count

func Count(ctx context.Context, c *mongo.Collection, filter any, opts ...*options.CountOptions) (int64, error)

func DecodeAll

func DecodeAll(ctx context.Context, cur *mongo.Cursor, docs any) error

func DecodeOne

func DecodeOne(r *mongo.SingleResult, doc any) error

func Delete

func Delete(ctx context.Context, c *mongo.Collection, filter any, opts ...*options.DeleteOptions) error

func DeleteMany

func DeleteMany(ctx context.Context, c *mongo.Collection, filter any, opts ...*options.DeleteOptions) error

func Find

func Find[T any](ctx context.Context, c *mongo.Collection, filter any, opts ...*options.FindOptions) (docs []T, err error)

func FindByCriteria

func FindByCriteria[T any](ctx context.Context, c *mongo.Collection, criteria Criteria, opts ...*options.FindOptions) ([]T, error)

func FindOne

func FindOne[T any](ctx context.Context, c *mongo.Collection, filter any, opts ...*options.FindOneOptions) (doc T, err error)

func GetClient

func GetClient(ctx context.Context, uri string) (*mongo.Client, error)

func GetDB

func GetDB(ctx context.Context, uri string) (*mongo.Database, error)

func GetDBName

func GetDBName(uri string) (string, error)

func Pagination

func Pagination(index int64, size int64) *options.FindOptions

func Parse

func Parse(values url.Values) map[string]Filter

func Save

func Save(ctx context.Context, c *mongo.Collection, filter any, update any, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)

func SaveMany

func SaveMany(ctx context.Context, c *mongo.Collection, filter any, update any, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)

func ToBson

func ToBson(doc any) (bson.M, error)

Types

type Criteria

type Criteria struct {
	Filter any
	Sort   any
	Index  int64
	Size   int64
}

func C

func C(values url.Values, filterName string) Criteria

func GetC

func GetC(query, filterName string) (Criteria, error)

func GetCriteria

func GetCriteria(query, filterName string) (Criteria, error)

func MustC

func MustC(query, filterName string) Criteria

func MustGetCriteria

func MustGetCriteria(query, filterName string) Criteria

MustGetCriteria query example: index=20&size=20&sort[]=sku&sort[]=-amount&filters[0][0][field]=sku&filters[0][0][value]=WSH%2529%25&filters[0][0][condition]=like&filters[0][1][field]=sku&filters[0][1][value]=WP%2529%25&filters[0][1][condition]=like&filters[1][0][field]=price&filters[1][0][value]=40&filters[1][0][condition]=eq&filters[2][0][field]=price&filters[2][0][value]=49.99&filters[2][0][condition]=to

func ToCriteria

func ToCriteria(values url.Values, filterName string) Criteria

type Expr

type Expr interface {
	Build() any
}

func All

func All(name string, value ...any) Expr

func And

func And(expr ...Expr) Expr

func ElemMatch

func ElemMatch(name string, query ...Expr) Expr

func Eq

func Eq(name string, value any) Expr

func Exists

func Exists(name string, value bool) Expr

func Gt

func Gt(name string, value any) Expr

func Gte

func Gte(name string, value any) Expr

func In

func In(name string, value ...any) Expr

func Lt

func Lt(name string, value any) Expr

func Lte

func Lte(name string, value any) Expr

func Ne

func Ne(name string, value any) Expr

func Nin

func Nin(name string, value ...any) Expr

func Nor

func Nor(expr ...Expr) Expr

func Not

func Not(name string, expr Expr) Expr

func Or

func Or(expr ...Expr) Expr

func Regex

func Regex(name string, pattern string, opts ...string) Expr

func Size

func Size(name string, value uint) Expr

type Field

type Field struct {
	Op    Operator
	Name  string
	Value any
}

func (*Field) Build

func (f *Field) Build() any

type Filter

type Filter []Expr

func ToFilter

func ToFilter(values url.Values, name string) Filter

func (Filter) Build

func (f Filter) Build() any

type Logical

type Logical struct {
	Op   Operator
	Data []Expr
}

func (*Logical) Build

func (l *Logical) Build() any

type Operator

type Operator string

Jump to

Keyboard shortcuts

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