vmdb

package
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2022 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Database connection for mongo db. The database is accessable by using the DB variable. requires DB_HOST, DB_PORT, DB_NAME

Index

Constants

This section is empty.

Variables

DB store the current database connection for use it global in your project.a

View Source
var ErrMongoDelete = errors.New("no delete document")

ErrMongoDelete represents an delete error in mongo case

View Source
var ErrMongoUpdate = errors.New("no updated document")

ErrMongoUpdate represents an update error in mongo case

Functions

func Connect

func Connect()

Connect connects the DB named database controller with an mongodb database

Types

type Collection

type Collection struct {
	Name     string
	Database *Database
}

Collection represents an mongo db database collection

func NewCollection

func NewCollection(name string) *Collection

NewCollection return a new Collection.

func (*Collection) Aggregate

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

Aggregate provide using aggregations.

func (*Collection) CreateIndex

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

CreateIndex creates an index for a given collection.

func (*Collection) DeleteOne

func (i *Collection) DeleteOne(ctx context.Context, filter bson.M) (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.M, value interface{}) (err error)

Find return a list of objects by given filter

func (*Collection) FindOne

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

FindOne select an element form database by using a given filter. The element will bind to the value interface{}. In error case it return an MongoError as error.

func (*Collection) UpdateOne

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

UpdateOne updates a value via "$set" and the given bson.M filter. Return an MongoError in case that no element has updated.

type Database

type Database struct {
	Host string
	Port string
	Name string
	DB   *mongo.Database
}

Database represents the mongo database connection object.

type Modified

type Modified struct {
	Updated int64 `json:"updated" bson:"updated`
	Created int64 `json:"created" bson:"created"`
	// contains filtered or unexported fields
}

Modified represents update and create timestamp for all models.

func NewModified

func NewModified() *Modified

NewModified initial Modified model, cTime is the current time Unix format.

func (*Modified) Update

func (i *Modified) Update()

Update set new Updated

type MongoError

type MongoError struct {
	Err        error       `json:"-" bson:"-"`
	Message    string      `json:"message" bson:"message"`
	Filter     interface{} `json:"filter" bson:"filter"`
	Value      interface{} `json:"value" bson:"value"`
	Database   string      `json:"database" bson:"database"`
	Collection string      `json:"collection" bson:"collection"`
	Line       int         `json:"line" bson:"line"`
	File       string      `json:"file" bson:"file"`
}

MongoError represents the struct of an mongo Error type.

func NewMongoError

func NewMongoError(err error, value interface{}, filter bson.M, database string, collection string) *MongoError

NewMongoError creates an mongo Error for an given parameter set

func (*MongoError) Error

func (i *MongoError) Error() string

Error return string of the error

Jump to

Keyboard shortcuts

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