mongo

package
v1.11.0 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2021 License: MIT Imports: 6 Imported by: 1

Documentation

Index

Constants

View Source
const (
	Equal            = "Equal"
	LessThan         = "LessThan"
	LessThanEqual    = "LessThanEqual"
	GreaterThan      = "GreaterThan"
	GreaterThanEqual = "GreaterThanEqual"

	Descending = "Descending"
	Ascending  = "Ascending"
)
View Source
const (
	ErrDuplicateCode = 11000
)

Variables

This section is empty.

Functions

func IsDuplicate

func IsDuplicate(err error) bool

IsDuplicate check error from mongo if error because duplicated record

func IsInvalidIndexValue

func IsInvalidIndexValue(err error) bool

IsInvalidIndexValue check error from mongo if error because invalid index value

func TranslateQuery

func TranslateQuery(query *Query) (bson.D, *options.FindOptions)

TranslateQuery struct to mongo bson

Types

type Config

type Config struct {
	Address     string
	Username    string
	Password    string
	Database    string
	MaxPoolSize int
}

Config struct to create new mongo db connection client

{
	Address: the URI of mongo db (mongodb://localhost:27017)
	Username: the username for authentication.
	Password: the password for authentication.
	Database: the name of the database to use for authentication.
	MaxPoolSize: specifies that maximum number of connections allowed in the driver's connection pool to each server.
}

func (Config) Client

func (m Config) Client() (*mongo.Client, error)

Client singleton of Mongo DB client connector, set MongoDB struct to call this method library with go.mongodb.org/mongo-driver/mongo

func (Config) Ping

func (m Config) Ping() error

Ping mongodb connection to check database status

func (Config) Reset

func (m Config) Reset()

Reset singleton mongo db connection client

type Filter

type Filter struct {
	Condition string
	Field     string
	Value     interface{}
}

Filter query for mongo

func NewFilter

func NewFilter(field, condition string, value interface{}) *Filter

NewFilter creates a new property filter

type Ordering

type Ordering struct {
	Field     string
	Direction string
}

Ordering set ordering field

func NewOrdering

func NewOrdering(field, direction string) *Ordering

NewOrdering query

type Query

type Query struct {
	Limit     int
	Offset    int
	Filters   []*Filter
	Orderings []*Ordering
}

Query struct to prepare query in mongo

func NewQuery

func NewQuery(limit, offset int) *Query

NewQuery for mongo

func (*Query) Filter

func (q *Query) Filter(property, condition string, value interface{}) *Query

Filter adds a filter to the query

func (*Query) Ordering

func (q *Query) Ordering(property, direction string) *Query

Ordering adds a sort order to the query

func (*Query) Slice

func (q *Query) Slice(offset, limit int) *Query

Slice result from mongo

Jump to

Keyboard shortcuts

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