mongo

package module
v0.0.19-dev Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2025 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const ID = "_id"

ID is the default MongoDB document ID field name.

Variables

This section is empty.

Functions

func CloseConnection

func CloseConnection() error

func FilterSearch

func FilterSearch(search string, fields ...string) primitive.M

FilterSearch builds a case-insensitive `$or` regex filter for the specified fields.

Example:

FilterSearch("john", "name", "email")
=>
{
  "$or": [
    {"name": {"$regex": "john", "$options": "i"}},
    {"email": {"$regex": "john", "$options": "i"}}
  ]
}

func NewConnection

func NewConnection(c *Config, l *zap.Logger) error

NewConnection sets up the MongoDB client and database.

func RequestSort

func RequestSort(sort []string) primitive.D

RequestSort converts a list of sort keys into a MongoDB sort document.

Prefixing a field with "-" sorts descending. Use "__" to represent nested fields. The special case "id" will be converted to "_id".

Example:

RequestSort([]string{"-created_at", "user__name"})
=>
bson.D{
  {"created_at", -1},
  {"user.name", 1},
}

func StructFilter

func StructFilter(m any, fields ...string) primitive.M

StructFilter returns a BSON map of specific fields from a struct. If no fields are specified, it returns all fields.

Types

type BaseRepository

type BaseRepository[T any] struct {
	Collection *Collection
	Context    context.Context
	// contains filtered or unexported fields
}

func NewBaseRepository

func NewBaseRepository[T any](col *Collection, searchFields []string, enableSoftDelete bool) *BaseRepository[T]

func (*BaseRepository[T]) FindAll

func (r *BaseRepository[T]) FindAll(opts *common.QueryOption, query CustomQueryFn) ([]*T, int64, error)

func (*BaseRepository[T]) FindByID

func (r *BaseRepository[T]) FindByID(id any) (*T, error)

func (*BaseRepository[T]) FindOne

func (r *BaseRepository[T]) FindOne(customQuery CustomQueryFn) (*T, error)

func (*BaseRepository[T]) Insert

func (r *BaseRepository[T]) Insert(entity *T) error

func (*BaseRepository[T]) SoftDelete

func (r *BaseRepository[T]) SoftDelete(id any) error

func (*BaseRepository[T]) Update

func (r *BaseRepository[T]) Update(entity *T, fields ...string) error

func (*BaseRepository[T]) WithContext

func (r *BaseRepository[T]) WithContext(ctx context.Context) common.BaseRepositoryInterface[T]

type Collection

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

Collection wraps a MongoDB collection with a default context for convenience.

func NewCollection

func NewCollection(name string, opts ...*options.CollectionOptions) *Collection

NewCollection creates and returns a new Collection from the default DB with the given name and options. The returned Collection uses the global default context, but you can override it with WithContext.

func (*Collection) Count

func (c *Collection) Count(filter any) (int64, error)

Count returns the number of documents matching the given filter. Returns the count and any error encountered.

func (*Collection) Create

func (c *Collection) Create(model Model, opts ...*options.InsertOneOptions) error

Create inserts the given model into the collection. Optionally accepts InsertOneOptions. On success, sets the inserted ID back to the model. Returns error if insertion fails.

func (*Collection) Delete

func (c *Collection) Delete(model Model) error

Delete removes a document matching the model's ID from the collection. Returns error if deletion fails.

func (*Collection) Finds

func (c *Collection) Finds(results any, filter any, opts ...*options.FindOptions) error

Finds executes a find query with the given filter and options, and decodes all results into 'results' (must be a pointer to a slice). Returns error if the find or decoding fails.

func (*Collection) GetOne

func (c *Collection) GetOne(filter any, model Model, opts ...*options.FindOneOptions) error

GetOne finds a single document matching the given filter and decodes it into the model. Returns error if not found.

func (*Collection) Show

func (c *Collection) Show(id any, model Model, opts ...*options.FindOneOptions) error

Show finds a document by its ID (string or ObjectID) and decodes it into 'model'. Optional FindOneOptions can be passed. Returns an error if the document is not found or the ID is invalid.

func (*Collection) Update

func (c *Collection) Update(model Model, fields ...string) error

Update updates only the specified fields of the given model document by ID. Returns error if the update fails.

func (*Collection) WithContext

func (c *Collection) WithContext(ctx context.Context) *Collection

WithContext sets a new context for the Collection and returns itself for chaining.

type Config

type Config struct {
	Server     string
	Username   string
	Password   string
	Database   string
	Datasource string
	CtxTimeout time.Duration
}

func ConfigDefault

func ConfigDefault(db string) *Config

type CustomQueryFn

type CustomQueryFn func(filter bson.M) bson.M

type Model

type Model any

Model is an alias for any struct representing a MongoDB document.

Jump to

Keyboard shortcuts

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