mongo

package
v1.1.4 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2023 License: GPL-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package mongo implement common logic interaction with mongodb

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseMongoRepository

type BaseMongoRepository interface {
	db.BaseRepository
	// Database return current mongo database
	Database() *mongo.Database
	// GetCollection returns reference to mongo.Collection (table)
	GetCollection(name string) *mongo.Collection
	// InsertOne executes an insert command to insert a single document into the collection.
	InsertOne(ctx context.Context, coll *mongo.Collection, document interface{}) (string, error)
	// GetOne returns document looked up by filter, or error
	GetOne(ctx context.Context, coll *mongo.Collection, filter interface{}, document interface{}) error
	// GetOneByID returns document looked up by id, or error
	GetOneByID(ctx context.Context, coll *mongo.Collection, id string, document interface{}) error
	// Delete deletes a stored document(s) by provided filter
	Delete(ctx context.Context, coll *mongo.Collection, filter interface{}) error
	// DeleteByID deletes a stored document with provided id
	DeleteByID(ctx context.Context, coll *mongo.Collection, id string) error
	// Find returns all documents matching to the filter
	Find(ctx context.Context, coll *mongo.Collection, filter interface{}, docs interface{}) error
	// ReplaceOne replace a single document looked up by filter
	ReplaceOne(ctx context.Context, coll *mongo.Collection, filter interface{}, document interface{}) error
	// UpdateOne updates a fields in single document looked up by filter
	UpdateOne(ctx context.Context, coll *mongo.Collection, filter interface{}, update interface{}) error
	// Count returns count of documents looked up by filter
	Count(ctx context.Context, coll *mongo.Collection, filter interface{}) (int64, error)
	// CollectionStats returns general statistics about mongodb collection
	CollectionStats(ctx context.Context, coll *mongo.Collection) (*CollectionStats, error)
}

BaseMongoRepository base MongoDb repository functionality

type CollectionStats added in v1.1.3

type CollectionStats struct {
	// Size is collection size in bytes
	Size int64 `json:"size"`
	//AvgObjSize is Average object size in bytes
	AvgObjSize int `json:"avgObjSize"`
	//StorageSize is storage size in bytes
	StorageSize int64 `json:"storageSize"`
	//TotalIndexSize isTotal index size in bytes
	TotalIndexSize int64 `json:"totalIndexSize"`
}

CollectionStats collection statistics

type DBResult

type DBResult map[string]interface{}

DBResult DB result from custom queries

type Db

type Db struct {
	Timeout time.Duration
	BaseMongoRepository
	// contains filtered or unexported fields
}

Db service managing connection to MongoDb instance

func NewMongoDB

func NewMongoDB(ctx context.Context, lgr logger.Logger, connectString string) (*Db, error)

NewMongoDB create a new Db instance, with the connection URI provided

func (*Db) Aggregate

func (db *Db) Aggregate(ctx context.Context, coll *mongo.Collection, pipe interface{}, opts *options.AggregateOptions, documents interface{}) error

Aggregate execute custom aggregate query

func (*Db) CollectionStats added in v1.1.3

func (db *Db) CollectionStats(ctx context.Context, coll *mongo.Collection) (*CollectionStats, error)

CollectionStats returns general statistics about mongodb collection

func (*Db) Count

func (db *Db) Count(ctx context.Context, coll *mongo.Collection, filter interface{}) (int64, error)

Count returns count of documents looked up by filter

func (*Db) Database

func (db *Db) Database() *mongo.Database

Database return current mongo database

func (*Db) Delete

func (db *Db) Delete(ctx context.Context, coll *mongo.Collection, filter interface{}) error

Delete deletes a stored document(s) looked up by provided filter

func (*Db) DeleteByID

func (db *Db) DeleteByID(ctx context.Context, coll *mongo.Collection, id string) error

DeleteByID deletes a stored document

func (*Db) Disconnect

func (db *Db) Disconnect(ctx context.Context) error

Disconnect close sockets to DB

func (*Db) Find

func (db *Db) Find(ctx context.Context, coll *mongo.Collection, filter interface{}, docs interface{}) error

Find returns all documents matching to the filter

func (*Db) GetCollection

func (db *Db) GetCollection(name string) *mongo.Collection

GetCollection returns reference to mongo.Collection (table)

func (*Db) GetOne

func (db *Db) GetOne(ctx context.Context, coll *mongo.Collection, filter interface{}, document interface{}) error

GetOne returns document looked up by filter, or error

func (*Db) GetOneByID

func (db *Db) GetOneByID(ctx context.Context, coll *mongo.Collection, id string, document interface{}) error

GetOneByID returns document looked up by id, or error

func (*Db) InsertOne

func (db *Db) InsertOne(ctx context.Context, coll *mongo.Collection, document interface{}) (string, error)

InsertOne executes an insert command to insert a single document into the collection.

func (*Db) Ping

func (db *Db) Ping(ctx context.Context) error

Ping check connection to database

func (*Db) ReplaceOne

func (db *Db) ReplaceOne(ctx context.Context, coll *mongo.Collection, filter interface{}, document interface{}) error

ReplaceOne replace a single document looked up by filter

func (*Db) UpdateOne

func (db *Db) UpdateOne(ctx context.Context, coll *mongo.Collection, filter interface{}, update interface{}) error

UpdateOne updates a fields in single document looked up by filter

Jump to

Keyboard shortcuts

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