mongo

package
v0.0.0-...-96967a2 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Get              dbOperation = "Get"
	GetOne           dbOperation = "GetOne"
	GetSorted        dbOperation = "GetSorted"
	GetAggregate     dbOperation = "GetAggregate"
	Insert           dbOperation = "Insert"
	InsertMany       dbOperation = "InsertMany"
	Upsert           dbOperation = "Upsert"
	Replace          dbOperation = "Replace"
	Count            dbOperation = "Count"
	GetCursor        dbOperation = "GetCursor"
	BulkUpsert       dbOperation = "BulkUpsert"
	BulkWrite        dbOperation = "BulkWrite"
	FindOneAndUpdate dbOperation = "FindOneAndUpdate"
	DeleteMany       dbOperation = "DeleteMany"
	GetDistinct      dbOperation = "GetDistinct"
	Delete           dbOperation = "Delete"
)

Variables

View Source
var NoItemFound = mongo.ErrNoDocuments

Functions

This section is empty.

Types

type AggregateKey

type AggregateKey struct {
	Key      string
	Operator AggregateOperator
	Value    string
}

AggregateKey represents a fiedl which needs to be aggregated

type AggregateKeys

type AggregateKeys []AggregateKey

AggregateKeys : array of AggregateKey

func (*AggregateKeys) Append

func (a *AggregateKeys) Append(aggregateKeys ...AggregateKey)

Append append function

type AggregateOperator

type AggregateOperator uint16

AggregateOperator operators used

const (
	SUM AggregateOperator = 1
	MIN AggregateOperator = 2
	MAX AggregateOperator = 3
)

type BaseMongoDBClient

type BaseMongoDBClient interface {
	Insert(ctx context.Context, table string, data interface{}) (err error)
	InsertMany(ctx context.Context, table string, data []interface{}) (err error)
	Get(ctx context.Context, table string, filters Filters, offset string, limit int64, result interface{}) (next string, err error)
	GetSorted(ctx context.Context, table string, filters Filters, offset string, limit int64, sortKey []SortKey, results interface{}) (next string, err error)
	GetOne(ctx context.Context, table string, filters Filters, result interface{}) (err error)
	Update(ctx context.Context, table string, filters Filters, updates Updates) (err error)
	BulkWrite(ctx context.Context, table string, data []mongo.WriteModel) (*mongo.BulkWriteResult, error)
	Upsert(ctx context.Context, table string, filters Filters, updates Updates) (err error)
	Replace(ctx context.Context, table string, filters Filters, data interface{}) (err error)
	Count(ctx context.Context, table string, filters Filters) (count int, err error)
	Delete(ctx context.Context, table string, filters Filters) (deletedCount int64, err error)
	GetReaderDB() (db interface{}, err error)
	GetWriterDB() (db interface{}, err error)
	FindOneAndUpdate(ctx context.Context, table string, filters Filters, updates Updates, result interface{}) (err error)
	GetCursor(ctx context.Context, table string, filters Filters, sortKeys []SortKey, projections interface{}) (cursor interface{}, err error)
	GetAggregate(ctx context.Context, collection string, filters Filters, groupKeys GroupKeys, aggregateKeys AggregateKeys) (cursor interface{}, err error)
	DeleteMany(ctx context.Context, collection string, filters Filters) (deletedCount int64, err error)
	Distinct(ctx context.Context, collection string, fieldName string, filters Filters) (result []interface{}, err error)
}

type BulkWriteResult

type BulkWriteResult struct {
	mongo.BulkWriteResult
	Err error
}

type DataType

type DataType uint16

DataType datatypes used

const (
	INT64        DataType = 1
	STRING       DataType = 2
	STRING_ARRAY DataType = 3
	INT          DataType = 4
	INT64_ARRAY  DataType = 5
	UINT64_ARRAY DataType = 6
	FLOAT64      DataType = 7
	TIME         DataType = 8
	BOOL         DataType = 9
)

type Filter

type Filter struct {
	Key      string
	Value    interface{}
	Type     DataType
	Operator Operator
}

Filter used to filter on fetch data

type Filters

type Filters []Filter

Filters : array of Filter

func (*Filters) Append

func (f *Filters) Append(filters ...Filter)

Append append function

type GroupKey

type GroupKey struct {
	Key   string
	Value interface{}
}

GroupKey represents a field by which we group

type GroupKeys

type GroupKeys []GroupKey

GroupKeys : array of GroupKey

func (*GroupKeys) Append

func (g *GroupKeys) Append(groupKeys ...GroupKey)

Append append function

type MongoDB

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

func NewMongoDB

func NewMongoDB(ctx context.Context, dbName, clientURI string) *MongoDB

func (*MongoDB) BulkWrite

func (db *MongoDB) BulkWrite(ctx context.Context, collection string, data []mongo.WriteModel) (*mongo.BulkWriteResult, error)

func (*MongoDB) Count

func (db *MongoDB) Count(ctx context.Context, collection string, filters Filters) (int, error)

Count ...

func (*MongoDB) Delete

func (db *MongoDB) Delete(ctx context.Context, collection string, filters Filters) (deletedCount int64, err error)

func (*MongoDB) DeleteMany

func (db *MongoDB) DeleteMany(ctx context.Context, collection string, filters Filters) (deletedCount int64, err error)

func (*MongoDB) Distinct

func (db *MongoDB) Distinct(ctx context.Context, collection string, fieldName string, filters Filters) ([]interface{}, error)

func (*MongoDB) FindOneAndUpdate

func (db *MongoDB) FindOneAndUpdate(ctx context.Context, collection string, filters Filters, updates Updates, result interface{}) error

FindOneAndUpdate

func (*MongoDB) Get

func (db *MongoDB) Get(ctx context.Context, collection string, filters Filters, offset string, limit int64, results interface{}) (string, error)

Get ...

func (*MongoDB) GetAggregate

func (db *MongoDB) GetAggregate(ctx context.Context, collection string, filters Filters, groupKeys GroupKeys, aggregateKeys AggregateKeys) (interface{}, error)

func (*MongoDB) GetCursor

func (db *MongoDB) GetCursor(ctx context.Context, collection string, filters Filters, sortKeys []SortKey, projections interface{}) (interface{}, error)

GetCursor ...

func (*MongoDB) GetOne

func (db *MongoDB) GetOne(ctx context.Context, collection string, filters Filters, projections []Projection, result interface{}) error

GetOne ...

func (*MongoDB) GetReaderDB

func (db *MongoDB) GetReaderDB() (interface{}, error)

GetReaderDB

func (*MongoDB) GetSorted

func (db *MongoDB) GetSorted(ctx context.Context, collection string, filters Filters, offset string, limit int64, sortKeys []SortKey, results interface{}) (string, error)

GetSorted ... eg. sortKeys := make(db.SortKeys)

sortKeys["created_at"] = db.DSC
sortKeys["name"] = db.ASC

func (*MongoDB) GetWriterDB

func (db *MongoDB) GetWriterDB() (interface{}, error)

GetWriterDB

func (*MongoDB) Insert

func (db *MongoDB) Insert(ctx context.Context, collection string, data interface{}) error

Insert ...

func (*MongoDB) InsertMany

func (db *MongoDB) InsertMany(ctx context.Context, collection string, data []interface{}) error

InsertMany ...

func (*MongoDB) Replace

func (db *MongoDB) Replace(ctx context.Context, collection string, filters Filters, data interface{}) error

Replace ...

func (*MongoDB) Update

func (db *MongoDB) Update(ctx context.Context, collection string, filters Filters, updates Updates) error

Update ...

func (*MongoDB) Upsert

func (db *MongoDB) Upsert(ctx context.Context, collection string, filters Filters, updates Updates) error

Update ...

type Operator

type Operator uint16

Operator operators used

const (
	EQUAL              Operator = 0
	IN                 Operator = 1
	GREATER_THAN_EQUAL Operator = 2
	LESS_THAN_EQUAL    Operator = 3
	GREATER_THAN       Operator = 4
	BETWEEN            Operator = 5
	IN_ARRAY           Operator = 6
	OR                 Operator = 7
	LESS_THAN          Operator = 8
	NOT_IN             Operator = 9
	ALL                Operator = 10
)

type Order

type Order int8
var (
	ASC Order = 1
	DSC Order = -1
)

type Projection

type Projection struct {
	Key   string
	Value interface{}
}

type Range

type Range struct {
	Left  interface{}
	Right interface{}
}

type SortKey

type SortKey struct {
	Key   string
	Order Order
}

SortKey is used to sort the result

type Update

type Update struct {
	Key            string
	Value          interface{}
	Type           DataType
	UpdateOperator UpdateOperator
}

Update used to Update data

type UpdateOperator

type UpdateOperator string

UpdateOperator operators used

const (
	SET      UpdateOperator = "$set"
	UNSET    UpdateOperator = "$unset"
	PUSH     UpdateOperator = "$push"
	PULL     UpdateOperator = "$pull"
	INC      UpdateOperator = "$inc"
	ARRAY_IN UpdateOperator = "$in"
	NOT      UpdateOperator = "$not"
)

func (UpdateOperator) ToString

func (u UpdateOperator) ToString() string

type Updates

type Updates []Update

Updates : array of Update

func (*Updates) Append

func (u *Updates) Append(updates ...Update)

Append append function

Jump to

Keyboard shortcuts

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