mongodb

package
v2.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2023 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrMsgDecode    = "failed to decode document due to error: %w"
	ErrMsgQuery     = "failed to execute query due to error: %w"
	ErrMsgMarshal   = "failed to marshal document due to error: %w"
	ErrMsgUnmarshal = "failed to unmarshal document due to error: %w"
)
View Source
const (
	ErrMsgClient   = "failed to create mongodb client due to error: %w"
	ErrMsgDatabase = "failed to create mongodb database due to error: %w"
)

Variables

View Source
var ErrNoDB = errors.New("database name not found in URI")
View Source
var Timeout = 5 * time.Second

Functions

func Count

func Count(ctx context.Context, c *mongo.Collection, filter any, opts ...*options.CountOptions) (int64, error)

func DecodeAll

func DecodeAll(ctx context.Context, cur *mongo.Cursor, docs any) error

func DecodeOne

func DecodeOne(r *mongo.SingleResult, doc any) error

func ExtractDatabaseName

func ExtractDatabaseName(uri string) (string, error)

func Find

func Find[T any](ctx context.Context, c *mongo.Collection, filter any, opts ...*options.FindOptions) (docs []T, err error)

func FindOne

func FindOne[T any](ctx context.Context, c *mongo.Collection, filter any, opts ...*options.FindOneOptions) (doc T, err error)

func NewClient

func NewClient(ctx context.Context, uri string) (*mongo.Client, error)

func Pagination

func Pagination(index int64, size int64) *options.FindOptions

func Remove

func Remove(ctx context.Context, c *mongo.Collection, filter any, opts ...*options.DeleteOptions) error

func RemoveMany

func RemoveMany(ctx context.Context, c *mongo.Collection, filter any, opts ...*options.DeleteOptions) error

func Save

func Save(ctx context.Context, c *mongo.Collection, filter any, update any, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)

func SaveMany

func SaveMany(ctx context.Context, c *mongo.Collection, filter any, update any, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)

func ToBson

func ToBson(doc any) (bson.M, error)

Types

type Config

type Config struct {
	URI  string `mapstructure:"uri"`
	Ping bool   `mapstructure:"ping"`
}

type Database

type Database struct {
	*mongo.Database
}

func NewDatabase

func NewDatabase(ctx context.Context, cfg *Config) (*Database, error)

func (*Database) Close

func (db *Database) Close(ctx context.Context) error

func (*Database) Ping

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

Jump to

Keyboard shortcuts

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