mongo

package
v0.1.0-20260312-120723... Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Collection

type Collection[T any] struct {
	// contains filtered or unexported fields
}

Collection is a generic wrapper around mongo.Collection that provides type-safe CRUD operations.

func GetCollection

func GetCollection[T any](m *MongoManager, collectionName string) *Collection[T]

GetCollection returns a new generic Collection instance for a specific document type.

func (*Collection[T]) Aggregate

Aggregate executes an aggregation pipeline. Note: Decoding the results is the responsibility of the caller, as aggregation results can have a different structure than the collection's document type T.

func (*Collection[T]) DeleteMany

func (c *Collection[T]) DeleteMany(ctx context.Context, filter bson.M, opts ...options.Lister[options.DeleteManyOptions]) (*mongo.DeleteResult, error)

DeleteMany deletes multiple documents matching the filter.

func (*Collection[T]) DeleteOne

func (c *Collection[T]) DeleteOne(ctx context.Context, filter bson.M, opts ...options.Lister[options.DeleteOneOptions]) (*mongo.DeleteResult, error)

DeleteOne deletes a single document matching the filter.

func (*Collection[T]) Find

func (c *Collection[T]) Find(ctx context.Context, filter bson.M, opts ...options.Lister[options.FindOptions]) ([]*T, error)

Find finds multiple documents matching the filter.

func (*Collection[T]) FindOne

func (c *Collection[T]) FindOne(ctx context.Context, filter bson.M, opts ...options.Lister[options.FindOneOptions]) (*T, error)

FindOne finds a single document matching the filter.

func (*Collection[T]) InsertMany

func (c *Collection[T]) InsertMany(ctx context.Context, documents []T) (*mongo.InsertManyResult, error)

InsertMany inserts multiple documents into the collection.

func (*Collection[T]) InsertOne

func (c *Collection[T]) InsertOne(ctx context.Context, document T) (*mongo.InsertOneResult, error)

InsertOne inserts a single document into the collection.

func (*Collection[T]) UpdateMany

func (c *Collection[T]) UpdateMany(ctx context.Context, filter, update bson.M, opts ...options.Lister[options.UpdateManyOptions]) (*mongo.UpdateResult, error)

UpdateMany updates multiple documents matching the filter.

func (*Collection[T]) UpdateOne

func (c *Collection[T]) UpdateOne(ctx context.Context, filter, update bson.M, opts ...options.Lister[options.UpdateOneOptions]) (*mongo.UpdateResult, error)

UpdateOne updates a single document matching the filter.

type MongoManager

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

MongoManager is the core struct that holds the MongoDB client and database connections.

func NewMongoManager

func NewMongoManager(opts ...MongoOption) (*MongoManager, error)

NewMongoManager creates a MongoManager with options

func (*MongoManager) Disconnect

func (m *MongoManager) Disconnect(ctx context.Context) error

Disconnect closes the MongoDB connection

func (*MongoManager) GetClient

func (m *MongoManager) GetClient() *mongo.Client

GetClient returns the underlying mongo.Client.

func (*MongoManager) GetDB

func (m *MongoManager) GetDB() *mongo.Database

GetDB returns the underlying mongo.Database.

func (*MongoManager) WithTransaction

func (m *MongoManager) WithTransaction(ctx context.Context, fn func(ctx context.Context) (any, error)) (any, error)

WithTransaction runs the given function within a MongoDB transaction. It handles starting the session, committing, and aborting the transaction automatically. The function `fn` receives a `mongo.SessionContext` which MUST be used for all operations within the transaction.

type MongoOption

type MongoOption func(*MongoManager) error

Option pattern for MongoManager configuration

func WithTimeout

func WithTimeout(d time.Duration) MongoOption

WithTimeout sets custom timeout

func WithURI

func WithURI(uri, dbName string) MongoOption

WithURI sets the MongoDB URI and database

Jump to

Keyboard shortcuts

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