mongodb

package
v0.8.2 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2025 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrFailedToCreateIndex = "failed to create index '%v': %v"
)

Variables

View Source
var (
	ErrFailedToCreateDocument = errors.New("failed to create document")
	ErrFailedToStartSession   = errors.New("failed to start session")
	ErrNilClient              = errors.New("mongodb client cannot be nil")
)

Functions

func CreateSession

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

CreateSession creates a new session

Parameters:

*client *mongo.Client: the MongoDB client

Returns:

(mongo.Session, error): the MongoDB session and an error if any

func CreateTransaction

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

CreateTransaction creates a new transaction

Parameters:

ctx context.Context: the context *client *mongo.Client: the MongoDB client *queries func(sc mongo.SessionContext) error: the queries to execute in the transaction

Returns:

error: an error if any

func CreateTransactionOptions

func CreateTransactionOptions() *options.TransactionOptions

CreateTransactionOptions creates the transaction options

Returns:

*options.TransactionOptions: the transaction options

func GetObjectIDFromString added in v0.7.5

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

GetObjectIDFromString gets the object ID from the string

Parameters:

  • id: the string ID to convert

Returns:

  • *primitive.ObjectID: the object ID
  • error: if any error occurred

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 any,
	sort any,
	upsert bool,
	returnDocument options.ReturnDocument,
) *options.FindOneAndUpdateOptions

PrepareFindOneAndUpdateOptions prepares the find one and update options

Parameters:

  • projection: The projection to apply to the query
  • sort: The sort to apply to the query
  • upsert: Whether to upsert the document if it doesn't exist
  • returnDocument: The return document option

Returns:

  • *options.FindOneAndUpdateOptions: The prepared find one and update options

func PrepareFindOneOptions

func PrepareFindOneOptions(
	projection any,
	sort any,
) *options.FindOneOptions

PrepareFindOneOptions prepares the find one options

Parameters:

  • projection: The projection to apply to the query
  • sort: The sort to apply to the query

Returns:

  • *options.FindOneOptions: The prepared find one options

func PrepareFindOptions

func PrepareFindOptions(
	projection any,
	sort any,
	limit int64,
	skip int64,
) *options.FindOptions

PrepareFindOptions prepares the find options

Parameters:

  • projection: The projection to apply to the query
  • sort: The sort to apply to the query
  • limit: The limit to apply to the query
  • skip: The skip to apply to the query

Returns:

  • *options.FindOptions: The prepared find options

func PrepareUpdateOptions

func PrepareUpdateOptions(upsert bool) *options.UpdateOptions

PrepareUpdateOptions prepares the update options

Parameters:

  • upsert: Whether to upsert the document if it doesn't exist

Returns:

  • *options.UpdateOptions: The prepared update options

Types

type Collection

type Collection struct {
	Indexes []*mongo.IndexModel
	// contains filtered or unexported fields
}

Collection represents a MongoDB collection

func NewCollection

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

NewCollection creates a new MongoDB collection

Parameters:

  • name: the name of the collection
  • indexes: the indexes to create for the collection

Returns:

  • *Collection: the new collection

func (Collection) CreateCollection

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

CreateCollection creates the collection

Parameters:

  • database: the MongoDB database

type Config

type Config struct {
	URI     string
	Timeout time.Duration
}

Config struct

func NewConfig added in v0.7.1

func NewConfig(uri string, timeout time.Duration) *Config

NewConfig creates a new MongoDB connection configuration

Parameters:

- uri: MongoDB URI - timeout: MongoDB connection timeout

Returns:

- *Config: MongoDB connection configuration

type DefaultHandler added in v0.7.1

type DefaultHandler struct {
	// contains filtered or unexported fields
}

DefaultHandler struct

func NewDefaultHandler added in v0.7.1

func NewDefaultHandler(config *Config) (
	*DefaultHandler,
	error,
)

NewDefaultHandler creates a new connection

Parameters:

  • config *Config: configuration for the connection

Returns:

  • *DefaultHandler: DefaultHandler struct
  • error: error if any

func (*DefaultHandler) Client added in v0.7.1

func (d *DefaultHandler) Client() (*mongo.Client, error)

Client returns the MongoDB client

Returns:

  • *mongo.Client: MongoDB client
  • error: error if any

func (*DefaultHandler) Connect added in v0.7.1

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

Connect returns a new MongoDB client

Returns:

  • *mongo.Client: MongoDB client
  • error: error if any

func (*DefaultHandler) Disconnect added in v0.7.1

func (d *DefaultHandler) Disconnect() error

Disconnect closes the MongoDB client connection

Returns:

  • error: error if any

func (*DefaultHandler) IsConnected added in v0.7.5

func (d *DefaultHandler) IsConnected() bool

IsConnected checks if the MongoDB client is connected

Returns:

  • bool: true if connected, false otherwise

type FieldIndex

type FieldIndex struct {
	// contains filtered or unexported fields
}

FieldIndex represents a field index

func NewFieldIndex

func NewFieldIndex(name string, order Order) *FieldIndex

NewFieldIndex creates a new field index

type Handler added in v0.7.1

type Handler interface {
	Connect() (*mongo.Client, error)
	IsConnected() bool
	Client() (*mongo.Client, error)
	Disconnect() error
}

Handler interface

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

Returns:

An integer representing the order

Jump to

Keyboard shortcuts

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