vmdb

package
v1.4.39 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2023 License: MIT Imports: 12 Imported by: 2

Documentation

Overview

Package vmdb

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrNoDocuments added in v1.4.1

func ErrNoDocuments(err error) bool

ErrNoDocuments return true if the error is an mongo.ErrNoDocuments error. Else the function returns false.

func UpdateInc added in v1.4.23

func UpdateInc(value interface{}) bson.D

func UpdateSet added in v1.4.6

func UpdateSet(value interface{}) bson.D

UpdateSet set all values in the database.

MongoDB:

{
	"$set": value,
	"$set": {
		"modified.updated": time.Now().Unix(),
	}
}

Types

type Collection

type Collection struct {
	Name         string
	DatabaseName string
	Collection   *mongo.Collection
}

Collection represents an mongo db database collection

func (*Collection) Aggregate

func (i *Collection) Aggregate(ctx context.Context, filter mongo.Pipeline, value interface{}) (err error)

Aggregate use the mongo.Collection.Aggregate function for select a list of elements using an aggregation pipeline.

func (*Collection) AggregateOne added in v1.4.1

func (i *Collection) AggregateOne(ctx context.Context, pipeline mongo.Pipeline, value interface{}) (err error)

AggregateOne use an aggregation pipeline for creating an struct that contains the information from more than one collection. If the result cursor contains objects, the first one will be decoded in the value param.

func (*Collection) CountDocuments added in v1.4.26

func (i *Collection) CountDocuments(ctx context.Context, filter bson.D) (count int64, err error)

CountDocuments count all documents filtered by the filter object.

func (*Collection) CreateIndex

func (i *Collection) CreateIndex(field string, unique bool) *Collection

CreateIndex creates an index for a given collection.

func (*Collection) CreateMultiIndex added in v1.4.0

func (i *Collection) CreateMultiIndex(filter bson.D, unique bool) *Collection

CreateMultiIndex creates an index for an value combination.

func (*Collection) DeleteMany added in v1.4.0

func (i *Collection) DeleteMany(ctx context.Context, filter bson.D) (err error)

DeleteMany deletes all elements from collection they match the filter object.

func (*Collection) DeleteOne

func (i *Collection) DeleteOne(ctx context.Context, filter bson.D) (err error)

DeleteOne deletes an element from given collection by the bson.M filter.

func (*Collection) Find

func (i *Collection) Find(ctx context.Context, filter bson.D, value interface{}) (err error)

Find use the mongo.Collection.Find function for select a list of elements from a collection. The result will decode in the value param. So the value need to be a slice struct.

func (*Collection) FindOne

func (i *Collection) FindOne(ctx context.Context, filter bson.D, value interface{}) (err error)

FindOne use the mongo.Collection.FindOne function for select one element from collection.

func (*Collection) InsertMany added in v1.4.0

func (i *Collection) InsertMany(ctx context.Context, value []interface{}) (err error)

InsertMany inserts a list of value and return an MongoError as error.

func (*Collection) InsertOne added in v1.4.0

func (i *Collection) InsertOne(ctx context.Context, value interface{}) (err error)

InsertOne inserts a value and return an MongoError as error.

func (*Collection) TryDeleteMany added in v1.4.1

func (i *Collection) TryDeleteMany(ctx context.Context, filter bson.D) (err error)

TryDeleteMany returns no error if no element was deleted.

func (*Collection) TryDeleteOne added in v1.4.1

func (i *Collection) TryDeleteOne(ctx context.Context, filter bson.D) (err error)

DeleteOne deletes an element from given collection by the bson.M filter.

func (*Collection) TryUpdateMany added in v1.4.1

func (i *Collection) TryUpdateMany(ctx context.Context, filter bson.A, value bson.M) (err error)

TryUpdateMany returns no error if no models was updated.

func (*Collection) TryUpdateOne added in v1.4.1

func (i *Collection) TryUpdateOne(ctx context.Context, filter bson.D, value interface{}) (err error)

TryUpdateOne returns no error if the model is not updated.

func (*Collection) UpdateMany added in v1.4.0

func (i *Collection) UpdateMany(ctx context.Context, filter bson.A, value bson.M) (err error)

UpdateMany updates an slice of interfaces. @TODO: create an result.

func (*Collection) UpdateOne

func (i *Collection) UpdateOne(ctx context.Context, filter bson.D, value interface{}, result interface{}) (err error)

UpdateOne use the mongo.Collection.UpdateOne function for update one element in an collection. If the result.MatchedCount == 0, the function returns an mongo.ErrNoDocuments error. If the result.MatchedCount != 0, the i.Collection.FindOne function is used to select the updated element and decode it into the result interface.

func (*Collection) UpdateOneAggregate added in v1.4.2

func (i *Collection) UpdateOneAggregate(ctx context.Context, filter bson.D, value interface{}, result interface{}, pipeline mongo.Pipeline) (err error)

UpdateOneAggregate works the same way than UpdateOne but you can define the pipeline param for decode an aggregated model into the result interface.

type Database

type Database struct {
	Name     string //Database Name
	URI      string //Database URI
	Database *mongo.Database
}

Database represents the initial struct for an Mongo Database connection named Name. Database is an pointer to an mongo.Database.

func NewDatabase added in v1.4.0

func NewDatabase(name string) *Database

func (*Database) Collection added in v1.4.0

func (i *Database) Collection(name string) *Collection

Collection initial an new mongodb collection named by the name parameter. Use NewDatabase for initial an database connection.

func (*Database) Connect added in v1.4.6

func (i *Database) Connect() *Database

Connect creates an mongo db client and initial an connection.

You can use the following parameters as environment variable or as flag to define the connection parameters.

MONGO_DB_HOST=<host>,
MONGO_DB_PORT=<port>,
MONGO_DB_USER=<user>,
MONGO_DB_PASSWORD=<password>

if the username or password is not defined, the Client try to connect without an user.

type DeletedResult added in v1.4.0

type DeletedResult struct {
	Model string `json:"model"`
	ID    string `json:"id"`
}

type Filter added in v1.4.1

type Filter bson.D

Filter represents an mongo filter object.

func NewFilter added in v1.4.1

func NewFilter() *Filter

NewMatch return an empty Match type.

func (*Filter) Append added in v1.4.1

func (i *Filter) Append(value bson.E)

func (*Filter) Bson added in v1.4.4

func (i *Filter) Bson() bson.D

Bson return the bson.D object.

func (*Filter) ContainsString added in v1.4.28

func (i *Filter) ContainsString(key string, value string)

ContainsString use regex for handling a substring matching.

MongoDB:

{
	key: {"$regex": .*value.*}
}

func (*Filter) ElemMatch added in v1.4.4

func (i *Filter) ElemMatch(list string, key string, value string)

ElemMatch TODO

func (*Filter) ElemMatchList added in v1.4.4

func (i *Filter) ElemMatchList(list string, key string, value []string)

ElemMatchList TODO

func (*Filter) EqualBool added in v1.4.4

func (i *Filter) EqualBool(key string, value string)

EqualBool the value is a string representation of an bool. match if value is equal to the value of the key in a database entry.

MongoDB:

{
	key: value as boolean
}

func (*Filter) EqualInt added in v1.4.1

func (i *Filter) EqualInt(key string, value string)

EqualInt the value is an string representation of an int. match if the value is equal to the value of the given key in an database entry.

MongoDB:

{
	key: value as int
}

func (*Filter) EqualInt64 added in v1.4.4

func (i *Filter) EqualInt64(key string, value string)

EqualInt the value is an string representation of an int64. match if value is equal to the value of the key in a database entry.

MongoDB:

{
	key: value as int64
}

func (*Filter) EqualString added in v1.4.1

func (i *Filter) EqualString(key string, value string)

EqualString match if the value is equal to the value of the key in a database collection.

MongoDB:

{
	key: value
}

func (*Filter) EqualStringList added in v1.4.1

func (i *Filter) EqualStringList(key string, value []string)

EqualStringList match if the the value of the key param is matching an element in the value param slice.

MongoDB:

{
	key: {"$or": value}
}

func (*Filter) ExpIn added in v1.4.4

func (i *Filter) ExpIn(key string, value string)

ExpIn TODO

func (*Filter) GteInt added in v1.4.1

func (i *Filter) GteInt(key string, value string)

GteInt provides $lte for key they have an int6 datatype. If the value element is "" or not an int formated string no element will be added to the filter object.

MongoDB:

{
	key: {"$gte": value as int}
}

func (*Filter) GteInt64 added in v1.4.1

func (i *Filter) GteInt64(key string, value string)

GteInt64 provides $gte for key they have an int64 datatype. If the value element is "" or not an int64 formated string no element will be added to the filter object.

MongoDB:

{
	key: {"$gte": value as int64}
}

func (*Filter) LikeString added in v1.4.4

func (i *Filter) LikeString(key string, value string)

LikeString use regex for handling a substring matching at the start of a string.

MongoDB:

{
	key: {"$regex": ^value}
}

func (*Filter) LteInt added in v1.4.1

func (i *Filter) LteInt(key string, value string)

LteInt provides $lte for key they have an int datatype. If the value element is "" or not an int formated string no element will be added to the filter object.

MongoDB:

{
	key: {"$lte": value as int}
}

func (*Filter) LteInt64 added in v1.4.1

func (i *Filter) LteInt64(key string, value string)

LteInt64 provides $lte for key they have an int64 datatype. If the value element is "" or not an int64 formated string no element will be added to the filter object.

MongoDB:

{
	key: {"$lte": value as int64}
}

func (*Filter) SearchString added in v1.4.28

func (i *Filter) SearchString(fields []string, value string) bson.D

SearchString searchs for a given string in all the given fields If the value search string is "" no search string will be added to the filter object.

MongoDB:

{
	key: {"$or": key: {"$regex": ".*value.*"}}
}

type Pipeline added in v1.4.0

type Pipeline struct {
	Pipe []bson.D
}

Pipeline represents an helper for handling mongodb pipeline. The Pipe param contains an []bson.D that represents an mongo pipeline.

func NewPipeline added in v1.4.0

func NewPipeline() *Pipeline

NewPipeline creates an new Pipeline struct.

func (*Pipeline) Append added in v1.4.17

func (i *Pipeline) Append(entry bson.D)

func (*Pipeline) AppendSlice added in v1.4.18

func (i *Pipeline) AppendSlice(pipe []bson.D)

Append appends the elements in pipe to the the Pipeline object.

func (*Pipeline) Count added in v1.4.32

func (i *Pipeline) Count() *Pipeline

Count adds the value total as $count to the end of the Pipeline struct.

MongoDB:

{"$count": total}

func (*Pipeline) Limit added in v1.4.25

func (i *Pipeline) Limit(value int64, defaultValue int64) *Pipeline

Limit is used for the mongo function $limit

func (*Pipeline) Lookup added in v1.4.0

func (i *Pipeline) Lookup(from string, localField string, foreignField string, as string)

Lookup represents an lookup to join an list of elements from a second collection to the result.

MongoDB:

{
	"$lookup":{
		"from": from,
		"localField": localField,
		"foreignField" foreignField,
		"as": as
	}
}

func (*Pipeline) LookupList added in v1.4.2

func (i *Pipeline) LookupList(from string, localField string, foreignField string, as string)

LookupList represents an lookup to join an list of elements from a second collection to the result. The value of the localField need to be a list of references. If the foreignField value is in the list, the element will joined to the as value.

MongoDB:

{
	"$lookup":{
		"from": from,
		"let": { localField: $localField },
		"foreignField" foreignField,
		"pipeline": [{
			"$match":{
				"$expr":{"$in: ["$foreinField", "$$localField"]}
			}
		}],
		"as": as
	}
}

func (*Pipeline) LookupMatch added in v1.4.1

func (i *Pipeline) LookupMatch(from string, localField string, foreignField string, as string, match bson.D)

LookupMatch represents an lookup to join an list of elements from a second collection to the result. The joined elements can be filtered by the match param.

MongoDB:

{
	"$lookup":{
		"from": from,
		"localField": localField,
		"foreignField" foreignField,
		"pipeline": [{"$match": match}]
		"as": as
	}
}

func (*Pipeline) LookupUnwind added in v1.4.0

func (i *Pipeline) LookupUnwind(from string, localField string, foreignField string, as string)

LockupUnwind represents the lookup and unwind combination to join an element from a second collection to the result.

MongoDB:

{
	"$lookup":{
		"from": from,
		"localField": localField,
		"foreignField" foreignField,
		"as": as
	},
	"$unwind": {
		"path": "$as",
		"preserveNullAndEmptyArrays": true
	}
}

func (*Pipeline) LookupUnwindMatch added in v1.4.1

func (i *Pipeline) LookupUnwindMatch(from string, localField string, foreignField string, as string, match bson.D)

LookupUnwindMatch represents the lookup and unwind combination to join an element from a second collection to the result. The joined element can be filtered by the match param.

MongoDB:

{
	"$lookup":{
		"from": from,
		"localField": localField,
		"foreignField" foreignField,
		"pipeline": [{"$match": match}]
		"as": as
	},
	"$unwind": {
		"path": "$as",
		"preserveNullAndEmptyArrays": true
	}
}

func (*Pipeline) Match added in v1.4.0

func (i *Pipeline) Match(filter bson.D) *Pipeline

Match adds the filter param as $match to the end of the Pipeline struct.

MongoDB:

{"$match": filter}

func (*Pipeline) Skip added in v1.4.25

func (i *Pipeline) Skip(value int64, defaultValue int64) *Pipeline

Skip used for the mongo function $skip

func (*Pipeline) Sort added in v1.4.25

func (i *Pipeline) Sort(value bson.D) *Pipeline

Sort is used for the mongo function $sort. Use Sort object for input. The fields for sorting have to be created via SortFields in first place due to performance (e.g. before lookup)

func (*Pipeline) SortFields added in v1.4.38

func (i *Pipeline) SortFields(sort bson.D) *Pipeline

Create case insensitive fields

type Sort added in v1.4.25

type Sort struct {
	Value bson.D
}

Sort represents an type for manage sorting in mongo db.

func NewSort added in v1.4.25

func NewSort() *Sort

NewSort creates a new Sort object.

func (*Sort) Add added in v1.4.25

func (i *Sort) Add(key string, value string)

Add adds an sort option to the sort object. The function convert ASC to 1 and DESC to -1.

func (*Sort) Bson added in v1.4.25

func (i *Sort) Bson() bson.D

Bson returns the bson object.

Jump to

Keyboard shortcuts

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