mongodb

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2025 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const EnvPrefix = "MONGO"

EnvPrefix environment prefix for mongodb config

View Source
const (
	TwoPrecision = 2
)

Variables

View Source
var (
	ErrConnectionIsNotSet = errors.New("connection is not set")
)

All kind of errors for mongo

Functions

func Clone

func Clone(oldObj interface{}) interface{}

func GetBsonD

func GetBsonD(keyValue ...interface{}) bson.D

GetBsonD return bson.D object based on values

func Set

func Set(r *MongoClient)

Set global client

Types

type Client

type Client interface {
	FindOne(
		ctx context.Context,
		collection string, value interface{}, filter interface{}, opts ...*options.FindOneOptions) error
	Find(
		ctx context.Context,
		collection string,
		value interface{},
		filter interface{},
		opts ...*options.FindOptions,
	) ([]interface{}, error)
	FindOneByID(
		ctx context.Context,
		collection string,
		value interface{},
		id interface{},
		opts ...*options.FindOneOptions,
	) error
	CountDocuments(
		ctx context.Context,
		collection string,
		filter interface{},
		opts ...*options.CountOptions,
	) (int64, error)
	Aggregate(
		ctx context.Context,
		collection string,
		value interface{},
		pipeline interface{},
		opts ...*options.AggregateOptions,
	) ([]interface{}, error)
	InsertOne(
		ctx context.Context,
		collection string,
		value interface{},
		opts ...*options.InsertOneOptions,
	) error
	UpsertOne(
		ctx context.Context,
		collection string,
		update interface{},
		filter interface{},
		opts ...*options.UpdateOptions,
	) error
	UpsertMany(
		ctx context.Context,
		collection string,
		keys []interface{},
		documents []interface{},
	) error
	UpsertManyByFilter(
		ctx context.Context,
		collection string,
		filterBy string,
		keys []interface{},
		documents []interface{},
	) error
	UpdateByObject(
		ctx context.Context,
		collection string,
		value interface{},
		filter interface{},
		opts ...*options.UpdateOptions,
	) error
	DeleteOne(ctx context.Context, collection string, filter interface{}, opts ...*options.DeleteOptions) error
	DeleteMany(ctx context.Context, collection string, filter interface{}, opts ...*options.DeleteOptions) error
	Drop(ctx context.Context, collection string) error
	InsertMany(ctx context.Context, collection string, documents []interface{}, opts ...*options.InsertManyOptions) error
	Collection(name string, opts ...*options.CollectionOptions) *mongo.Collection
	Database(name string, opts ...*options.DatabaseOptions) *mongo.Database
	WithDB(name string) Client
	Connection() *mongo.Client
	BatchUpdateByID(ctx context.Context, collection string, data map[interface{}]interface{}) error
	DeleteCollection(ctx context.Context, name string) error
}

type ConnectionConfig

type ConnectionConfig struct {
	Hosts         []string `env:"_HOSTS" envDefault:"localhost:27017"`
	User          string   `env:"_USER" envDefault:""`
	Pass          string   `env:"_PASS" envDefault:""`
	Scheme        string   `env:"_SCHEME" envDefault:"mongodb"`
	Database      string   `env:"_DATABASE" envDefault:"default"`
	Args          string   `env:"_ARGS" envDefault:""`
	Mode          string   `env:"_MODE" envDefault:"secondarypreferred"`
	RetryWrites   bool     `env:"_RETRYWRITES" envDefault:"false"`
	AuthMechanism string   `env:"_AUTH_MECHANISM" envDefault:"SCRAM-SHA-256"`
	AuthSource    string   `env:"_AUTH_SOURCE" envDefault:""`
}

ConnectionConfig contains required data for mongo

func GetConnectionConfigFromEnv

func GetConnectionConfigFromEnv() (*ConnectionConfig, error)

GetConnectionConfigFromEnv return mongodb configs bases on environment variables

func (*ConnectionConfig) GetDSN

func (c *ConnectionConfig) GetDSN() string

type MongoClient

type MongoClient struct {
	*mongo.Client
	// contains filtered or unexported fields
}

MongoClient client for mongo db

func Default

func Default() (*MongoClient, error)

Default return default client

func Get

func Get() (*MongoClient, error)

Get return mongo client

func NewMongoClient

func NewMongoClient(config *ConnectionConfig) (*MongoClient, error)

NewMongoClient return client from config

func (*MongoClient) Aggregate

func (m *MongoClient) Aggregate(
	ctx context.Context,
	collection string,
	value interface{},
	pipeline interface{},
	opts ...*options.AggregateOptions,
) ([]interface{}, error)

Aggregate aggregate rows

func (*MongoClient) BatchUpdateByID

func (m *MongoClient) BatchUpdateByID(ctx context.Context, collection string, data map[interface{}]interface{}) error

func (*MongoClient) Collection

func (m *MongoClient) Collection(name string, opts ...*options.CollectionOptions) *mongo.Collection

func (*MongoClient) Connection

func (m *MongoClient) Connection() *mongo.Client

func (*MongoClient) CountDocuments

func (m *MongoClient) CountDocuments(
	ctx context.Context,
	collection string,
	filter interface{},
	opts ...*options.CountOptions,
) (int64, error)

CountDocuments count documents

func (*MongoClient) Database

func (m *MongoClient) Database(name string, opts ...*options.DatabaseOptions) *mongo.Database

func (*MongoClient) DeleteCollection

func (m *MongoClient) DeleteCollection(ctx context.Context, name string) error

func (*MongoClient) DeleteMany

func (m *MongoClient) DeleteMany(
	ctx context.Context,
	collection string,
	filter interface{},
	opts ...*options.DeleteOptions,
) error

DeleteMany delete all elements by condition

func (*MongoClient) DeleteOne

func (m *MongoClient) DeleteOne(
	ctx context.Context,
	collection string,
	filter interface{},
	opts ...*options.DeleteOptions,
) error

DeleteOne delete one element

func (*MongoClient) Drop

func (m *MongoClient) Drop(ctx context.Context, collection string) error

Drop collection

func (*MongoClient) Find

func (m *MongoClient) Find(
	ctx context.Context,
	collection string,
	value interface{},
	filter interface{},
	opts ...*options.FindOptions,
) ([]interface{}, error)

Find all and return

func (*MongoClient) FindOne

func (m *MongoClient) FindOne(
	ctx context.Context,
	collection string,
	value interface{},
	filter interface{},
	opts ...*options.FindOneOptions,
) error

FindOne find and decode one element

func (*MongoClient) FindOneByID

func (m *MongoClient) FindOneByID(
	ctx context.Context,
	collection string,
	value interface{},
	id interface{},
	opts ...*options.FindOneOptions,
) error

FindOneByID return value by _id

func (*MongoClient) InsertMany

func (m *MongoClient) InsertMany(
	ctx context.Context,
	collection string,
	documents []interface{},
	opts ...*options.InsertManyOptions,
) error

InsertMany insert many documents

func (*MongoClient) InsertOne

func (m *MongoClient) InsertOne(
	ctx context.Context,
	collection string,
	value interface{},
	opts ...*options.InsertOneOptions,
) error

InsertOne insert single object

func (*MongoClient) UpdateByObject

func (m *MongoClient) UpdateByObject(
	ctx context.Context,
	collection string,
	value interface{},
	filter interface{},
	opts ...*options.UpdateOptions,
) error

UpdateByObject update to an object by filter

func (*MongoClient) UpsertMany

func (m *MongoClient) UpsertMany(
	ctx context.Context,
	collection string,
	keys []interface{},
	documents []interface{},
) error

UpsertMany insert or update objects

func (*MongoClient) UpsertManyByFilter

func (m *MongoClient) UpsertManyByFilter(
	ctx context.Context,
	collection string,
	filterBy string,
	keys []interface{},
	documents []interface{},
) error

UpsertManyByFilter insert or update objects by filter

func (*MongoClient) UpsertOne

func (m *MongoClient) UpsertOne(
	ctx context.Context,
	collection string,
	update interface{},
	filter interface{},
	opts ...*options.UpdateOptions,
) error

UpsertOne insert or update single object

func (*MongoClient) WithDB

func (m *MongoClient) WithDB(name string) Client

Directories

Path Synopsis
Package mock_mongodb is a generated GoMock package.
Package mock_mongodb is a generated GoMock package.

Jump to

Keyboard shortcuts

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