mongodb

package
v0.4.6 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrFailedToCreateDocument = errors.New("failed to create document")
	ErrFailedToStartSession   = errors.New("failed to start session")
	ErrFailedToCreateIndex    = "failed to create index '%v': %v"
	ErrNilConfig              = errors.New("mongodb connection config cannot be nil")
	ErrNilClient              = errors.New("mongodb client cannot be nil")
)

Functions

func CreateSession

func CreateSession(client *mongo.Client) (mongo.Session, error)

CreateSession creates a new session

func CreateTransaction

func CreateTransaction(
	client *mongo.Client,
	queries func(sc mongo.SessionContext) error,
) error

CreateTransaction creates a new transaction

func CreateTransactionOptions

func CreateTransactionOptions() *options.TransactionOptions

CreateTransactionOptions creates the transaction options

func GetObjectIdFromString

func GetObjectIdFromString(id string) (*primitive.ObjectID, error)

GetObjectIdFromString gets the object ID from the string

func NewCompoundFieldIndex

func NewCompoundFieldIndex(
	fieldIndexes []*FieldIndex, unique bool,
) *mongo.IndexModel

NewCompoundFieldIndex creates a new compound field index model

func NewTTLIndex

func NewTTLIndex(fieldName string, expireAfterSeconds int32) *mongo.IndexModel

NewTTLIndex creates a new TTL index model

func NewUniqueIndex

func NewUniqueIndex(fieldIndex FieldIndex, unique bool) *mongo.IndexModel

NewUniqueIndex creates a new unique field index model

func PrepareFindOneAndUpdateOptions added in v0.2.0

func PrepareFindOneAndUpdateOptions(
	projection interface{},
	sort interface{},
	upsert bool,
	returnDocument options.ReturnDocument,
) *options.FindOneAndUpdateOptions

PrepareFindOneAndUpdateOptions prepares the find one and update options

func PrepareFindOneOptions

func PrepareFindOneOptions(projection interface{}, sort interface{}) *options.FindOneOptions

PrepareFindOneOptions prepares the find one options

func PrepareFindOptions

func PrepareFindOptions(projection interface{}, sort interface{}, limit int64, skip int64) *options.FindOptions

PrepareFindOptions prepares the find options

func PrepareUpdateOptions

func PrepareUpdateOptions(upsert bool) *options.UpdateOptions

PrepareUpdateOptions prepares the update options

Types

type Collection

type Collection struct {
	Name    string
	Indexes *[]*mongo.IndexModel
}

Collection represents a MongoDB collection

func NewCollection

func NewCollection(
	name string,
	indexes *[]*mongo.IndexModel,
) *Collection

NewCollection creates a new MongoDB collection

func (*Collection) CreateCollection

func (c *Collection) CreateCollection(database *mongo.Database) (
	collection *mongo.Collection, err error,
)

CreateCollection creates the collection

type Config

type Config struct {
	Uri     string
	Timeout time.Duration
}

Config struct

type ConnectionHandler

type ConnectionHandler interface {
	Connect() (*mongo.Client, error)
	GetClient() (*mongo.Client, error)
	Disconnect()
}

ConnectionHandler interface

type DefaultConnectionHandler

type DefaultConnectionHandler struct {
	Ctx           context.Context
	Cancel        context.CancelFunc
	ClientOptions *options.ClientOptions
	Client        *mongo.Client
}

DefaultConnectionHandler struct

func NewDefaultConnectionHandler

func NewDefaultConnectionHandler(config *Config) (
	*DefaultConnectionHandler,
	error,
)

NewDefaultConnectionHandler creates a new connection

func (*DefaultConnectionHandler) Connect

func (d *DefaultConnectionHandler) Connect() (*mongo.Client, error)

Connect returns a new MongoDB client

func (*DefaultConnectionHandler) Disconnect

func (d *DefaultConnectionHandler) Disconnect()

Disconnect closes the MongoDB client connection

func (*DefaultConnectionHandler) GetClient

func (d *DefaultConnectionHandler) GetClient() (*mongo.Client, error)

GetClient returns the MongoDB client

type FieldIndex

type FieldIndex struct {
	Name  string
	Order Order
}

func NewFieldIndex

func NewFieldIndex(name string, order Order) *FieldIndex

NewFieldIndex creates a new field index

type Order

type Order int

Order represents the order of the index

const (
	// Ascending order
	Ascending Order = 1

	// Descending order
	Descending Order = -1
)

func (Order) OrderInt

func (o Order) OrderInt() int

OrderInt converts the Order type to an integer

Jump to

Keyboard shortcuts

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