Documentation
¶
Overview ¶
Package mongo_kit provides a MongoDB client wrapper with convenient methods for connection management and database operations.
Index ¶
- Variables
- type AggregationBuilder
- func (ab *AggregationBuilder) AddStage(stage bson.D) *AggregationBuilder
- func (ab *AggregationBuilder) Build() []bson.D
- func (ab *AggregationBuilder) Group(id any, fields bson.M) *AggregationBuilder
- func (ab *AggregationBuilder) Limit(limit int64) *AggregationBuilder
- func (ab *AggregationBuilder) Lookup(from, localField, foreignField, as string) *AggregationBuilder
- func (ab *AggregationBuilder) Match(filter any) *AggregationBuilder
- func (ab *AggregationBuilder) Project(projection any) *AggregationBuilder
- func (ab *AggregationBuilder) Skip(skip int64) *AggregationBuilder
- func (ab *AggregationBuilder) Sort(sort any) *AggregationBuilder
- func (ab *AggregationBuilder) Unwind(path string) *AggregationBuilder
- type Client
- type Config
- type ConfigError
- type ConnectionError
- type OperationError
- type Option
- type QueryBuilder
- func (qb *QueryBuilder) And(conditions ...bson.D) *QueryBuilder
- func (qb *QueryBuilder) AndConditions(builders ...*QueryBuilder) *QueryBuilder
- func (qb *QueryBuilder) Build() (bson.D, *options.FindOptions)
- func (qb *QueryBuilder) Equals(key string, value any) *QueryBuilder
- func (qb *QueryBuilder) Exists(key string, exists bool) *QueryBuilder
- func (qb *QueryBuilder) Filter(key string, value any) *QueryBuilder
- func (qb *QueryBuilder) GetFilter() bson.D
- func (qb *QueryBuilder) GreaterThan(key string, value any) *QueryBuilder
- func (qb *QueryBuilder) GreaterThanOrEqual(key string, value any) *QueryBuilder
- func (qb *QueryBuilder) In(key string, values ...any) *QueryBuilder
- func (qb *QueryBuilder) LessThan(key string, value any) *QueryBuilder
- func (qb *QueryBuilder) LessThanOrEqual(key string, value any) *QueryBuilder
- func (qb *QueryBuilder) Limit(limit int64) *QueryBuilder
- func (qb *QueryBuilder) Nor(conditions ...bson.D) *QueryBuilder
- func (qb *QueryBuilder) NorConditions(builders ...*QueryBuilder) *QueryBuilder
- func (qb *QueryBuilder) NotEquals(key string, value any) *QueryBuilder
- func (qb *QueryBuilder) NotIn(key string, values ...any) *QueryBuilder
- func (qb *QueryBuilder) Or(conditions ...bson.D) *QueryBuilder
- func (qb *QueryBuilder) OrConditions(builders ...*QueryBuilder) *QueryBuilder
- func (qb *QueryBuilder) Project(projection any) *QueryBuilder
- func (qb *QueryBuilder) Regex(key string, pattern string, options string) *QueryBuilder
- func (qb *QueryBuilder) Skip(skip int64) *QueryBuilder
- func (qb *QueryBuilder) Sort(field string, ascending bool) *QueryBuilder
- func (qb *QueryBuilder) SortBy(sort any) *QueryBuilder
- func (qb *QueryBuilder) Where(expression any) *QueryBuilder
- type Repository
- func (r *Repository[T]) Aggregate(ctx context.Context, pipeline any, opts ...*options.AggregateOptions) ([]T, error)
- func (r *Repository[T]) Collection() string
- func (r *Repository[T]) Count(ctx context.Context, filter any, opts ...*options.CountOptions) (int64, error)
- func (r *Repository[T]) CountAll(ctx context.Context, opts ...*options.CountOptions) (int64, error)
- func (r *Repository[T]) CountWithBuilder(ctx context.Context, qb *QueryBuilder) (int64, error)
- func (r *Repository[T]) Create(ctx context.Context, document T) (any, error)
- func (r *Repository[T]) CreateMany(ctx context.Context, documents []T) ([]any, error)
- func (r *Repository[T]) DeleteByID(ctx context.Context, id any) (*mongo.DeleteResult, error)
- func (r *Repository[T]) DeleteMany(ctx context.Context, filter any) (*mongo.DeleteResult, error)
- func (r *Repository[T]) DeleteOne(ctx context.Context, filter any) (*mongo.DeleteResult, error)
- func (r *Repository[T]) Drop(ctx context.Context) error
- func (r *Repository[T]) EstimatedCount(ctx context.Context, opts ...*options.EstimatedDocumentCountOptions) (int64, error)
- func (r *Repository[T]) Exists(ctx context.Context, filter any) (bool, error)
- func (r *Repository[T]) ExistsByID(ctx context.Context, id any) (bool, error)
- func (r *Repository[T]) ExistsWithBuilder(ctx context.Context, qb *QueryBuilder) (bool, error)
- func (r *Repository[T]) Find(ctx context.Context, filter any, opts ...*options.FindOptions) ([]T, error)
- func (r *Repository[T]) FindAll(ctx context.Context, opts ...*options.FindOptions) ([]T, error)
- func (r *Repository[T]) FindByID(ctx context.Context, id any) (*T, error)
- func (r *Repository[T]) FindOne(ctx context.Context, filter any, opts ...*options.FindOneOptions) (*T, error)
- func (r *Repository[T]) FindOneWithBuilder(ctx context.Context, qb *QueryBuilder) (*T, error)
- func (r *Repository[T]) FindWithBuilder(ctx context.Context, qb *QueryBuilder) ([]T, error)
- func (r *Repository[T]) UpdateByID(ctx context.Context, id any, update any, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
- func (r *Repository[T]) UpdateMany(ctx context.Context, filter any, update any, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
- func (r *Repository[T]) UpdateOne(ctx context.Context, filter any, update any, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
- func (r *Repository[T]) Upsert(ctx context.Context, filter any, update any) (*mongo.UpdateResult, error)
- type UpdateBuilder
- func (ub *UpdateBuilder) AddToSet(key string, value any) *UpdateBuilder
- func (ub *UpdateBuilder) Build() bson.D
- func (ub *UpdateBuilder) CurrentDate(key string) *UpdateBuilder
- func (ub *UpdateBuilder) Inc(key string, value any) *UpdateBuilder
- func (ub *UpdateBuilder) Max(key string, value any) *UpdateBuilder
- func (ub *UpdateBuilder) Min(key string, value any) *UpdateBuilder
- func (ub *UpdateBuilder) Mul(key string, value any) *UpdateBuilder
- func (ub *UpdateBuilder) Pop(key string, first bool) *UpdateBuilder
- func (ub *UpdateBuilder) Pull(key string, value any) *UpdateBuilder
- func (ub *UpdateBuilder) Push(key string, value any) *UpdateBuilder
- func (ub *UpdateBuilder) Rename(oldName string, newName string) *UpdateBuilder
- func (ub *UpdateBuilder) Set(key string, value any) *UpdateBuilder
- func (ub *UpdateBuilder) Unset(keys ...string) *UpdateBuilder
Constants ¶
This section is empty.
Variables ¶
var ( // ErrClientClosed is returned when an operation is attempted on a closed client. // Use errors.Is(err, mongo.ErrClientClosed) to check for this error. ErrClientClosed = errors.New("mongo: client is closed") )
Functions ¶
This section is empty.
Types ¶
type AggregationBuilder ¶
type AggregationBuilder struct {
// contains filtered or unexported fields
}
AggregationBuilder provides a fluent interface for building aggregation pipelines.
func NewAggregationBuilder ¶
func NewAggregationBuilder() *AggregationBuilder
NewAggregationBuilder creates a new AggregationBuilder instance.
func (*AggregationBuilder) AddStage ¶
func (ab *AggregationBuilder) AddStage(stage bson.D) *AggregationBuilder
AddStage adds a custom pipeline stage.
func (*AggregationBuilder) Build ¶
func (ab *AggregationBuilder) Build() []bson.D
Build returns the aggregation pipeline.
func (*AggregationBuilder) Group ¶
func (ab *AggregationBuilder) Group(id any, fields bson.M) *AggregationBuilder
Group adds a $group stage.
func (*AggregationBuilder) Limit ¶
func (ab *AggregationBuilder) Limit(limit int64) *AggregationBuilder
Limit adds a $limit stage.
func (*AggregationBuilder) Lookup ¶
func (ab *AggregationBuilder) Lookup(from, localField, foreignField, as string) *AggregationBuilder
Lookup adds a $lookup stage for joins.
func (*AggregationBuilder) Match ¶
func (ab *AggregationBuilder) Match(filter any) *AggregationBuilder
Match adds a $match stage.
func (*AggregationBuilder) Project ¶
func (ab *AggregationBuilder) Project(projection any) *AggregationBuilder
Project adds a $project stage.
func (*AggregationBuilder) Skip ¶
func (ab *AggregationBuilder) Skip(skip int64) *AggregationBuilder
Skip adds a $skip stage.
func (*AggregationBuilder) Sort ¶
func (ab *AggregationBuilder) Sort(sort any) *AggregationBuilder
Sort adds a $sort stage.
func (*AggregationBuilder) Unwind ¶
func (ab *AggregationBuilder) Unwind(path string) *AggregationBuilder
Unwind adds an $unwind stage.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client wraps the MongoDB driver client with convenience methods. It provides a simpler API for common operations while maintaining thread-safety. The client is safe for concurrent use across multiple goroutines.
While Client is exported for advanced use cases, users are encouraged to primarily interact with the database through Repository[T] for type-safe operations.
func New ¶
New creates a new MongoDB client with the given configuration. Configuration uses the functional options pattern for flexibility.
The client will automatically:
- Validate the configuration
- Connect to MongoDB
- Verify the connection with a ping
Example:
client, err := mongo_kit.New(
mongo_kit.DefaultConfig(),
mongo_kit.WithURI("mongodb://localhost:27017"),
mongo_kit.WithDatabase("myapp"),
mongo_kit.WithMaxPoolSize(200),
)
if err != nil {
log.Fatal(err)
}
defer client.Close(context.Background())
Returns an error if:
- Configuration is invalid
- Connection to MongoDB fails
- Ping verification fails
func (*Client) Close ¶
Close closes the MongoDB client connection gracefully. After calling Close, the client should not be used. Calling Close multiple times is safe and will only disconnect once.
Example:
defer client.Close(context.Background())
func (*Client) CreateCollection ¶
func (c *Client) CreateCollection(ctx context.Context, name string, opts ...*options.CreateCollectionOptions) error
CreateCollection creates a new collection with optional configuration. If the collection already exists, this is a no-op (no error is returned).
Example:
err := client.CreateCollection(ctx, "users", options.CreateCollection().SetCapped(true).SetSizeInBytes(1000000))
func (*Client) CreateIndexes ¶
func (c *Client) CreateIndexes(ctx context.Context, collection string, indexes []mongo.IndexModel) ([]string, error)
CreateIndexes creates multiple indexes on the specified collection. Returns the names of the created indexes.
Example:
indexes := []mongo.IndexModel{
{
Keys: bson.D{{Key: "email", Value: 1}},
Options: options.Index().SetUnique(true),
},
{
Keys: bson.D{{Key: "created_at", Value: -1}},
},
}
names, err := client.CreateIndexes(ctx, "users", indexes)
type Config ¶
type Config struct {
URI string // MongoDB connection URI (required)
Database string // Default database name (required)
MaxPoolSize uint64 // Maximum number of connections in the connection pool (default: 100)
Timeout time.Duration // Default timeout for all operations (default: 10s)
ClientOptions *options.ClientOptions // Direct access to MongoDB driver options for advanced use cases
}
Config holds the MongoDB client configuration. Use DefaultConfig() to get sensible defaults, then customize with Option functions.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a Config with sensible default values. This is the recommended starting point for most applications.
Default values:
- URI: "mongodb://localhost:27017"
- Database: "default"
- MaxPoolSize: 100
- Timeout: 10 seconds
Example:
cfg := mongo_kit.DefaultConfig()
client, err := mongo_kit.New(cfg, mongo_kit.WithURI("mongodb://prod:27017"))
type ConfigError ¶ added in v0.2.0
type ConfigError struct {
Field string // The configuration field that caused the error (optional)
Message string // Human-readable error message
}
ConfigError represents a configuration validation error. Users can access the Field and Message to understand what configuration is invalid.
func (*ConfigError) Error ¶ added in v0.2.0
func (e *ConfigError) Error() string
type ConnectionError ¶ added in v0.2.0
type ConnectionError struct {
Cause error // The underlying error that caused the connection failure
}
ConnectionError represents a connection failure to MongoDB. The Cause field contains the underlying error from the MongoDB driver.
func (*ConnectionError) Error ¶ added in v0.2.0
func (e *ConnectionError) Error() string
func (*ConnectionError) Unwrap ¶ added in v0.2.0
func (e *ConnectionError) Unwrap() error
type OperationError ¶ added in v0.2.0
type OperationError struct {
Op string // The name of the operation that failed (e.g., "find", "insert", "update")
Cause error // The underlying error from MongoDB driver
}
OperationError represents an error that occurred during a database operation. The Op field identifies which operation failed, and Cause contains the underlying error.
func (*OperationError) Error ¶ added in v0.2.0
func (e *OperationError) Error() string
func (*OperationError) Unwrap ¶ added in v0.2.0
func (e *OperationError) Unwrap() error
type Option ¶
type Option func(*Config)
Option is a function that modifies a Config. Use Option functions with New() to customize the configuration.
func WithClientOptions ¶ added in v0.2.0
func WithClientOptions(opts *options.ClientOptions) Option
WithClientOptions allows you to directly configure the underlying MongoDB driver options. This is an escape hatch for advanced configurations not covered by the basic options.
Use this when you need fine-grained control over:
- Minimum pool size
- Retry behavior
- TLS/SSL settings
- Compression
- Read preferences
- Write concerns
- And more...
Example:
clientOpts := options.Client() clientOpts.SetMinPoolSize(10) clientOpts.SetRetryWrites(true) clientOpts.SetReadPreference(readpref.Secondary()) mongo_kit.WithClientOptions(clientOpts)
Note: Settings applied via WithClientOptions will override basic options like MaxPoolSize.
func WithDatabase ¶
WithDatabase sets the default database name. All operations without an explicit database will use this database.
Example:
mongo_kit.WithDatabase("myapp")
func WithMaxPoolSize ¶
WithMaxPoolSize sets the maximum number of connections in the connection pool. Default is 100. Increase for high-concurrency applications.
Example:
mongo_kit.WithMaxPoolSize(200)
func WithTimeout ¶
WithTimeout sets the default timeout for all database operations. This timeout applies to operations that don't specify their own context timeout. Default is 10 seconds.
Example:
mongo_kit.WithTimeout(30 * time.Second)
type QueryBuilder ¶
type QueryBuilder struct {
// contains filtered or unexported fields
}
QueryBuilder provides a fluent interface for building MongoDB queries
func NewQueryBuilder ¶
func NewQueryBuilder() *QueryBuilder
NewQueryBuilder creates a new QueryBuilder instance.
func (*QueryBuilder) And ¶
func (qb *QueryBuilder) And(conditions ...bson.D) *QueryBuilder
And adds an and condition.
func (*QueryBuilder) AndConditions ¶ added in v0.2.0
func (qb *QueryBuilder) AndConditions(builders ...*QueryBuilder) *QueryBuilder
AndConditions combines multiple QueryBuilders with $and logic.
func (*QueryBuilder) Build ¶
func (qb *QueryBuilder) Build() (bson.D, *options.FindOptions)
Build returns the filter and options.
func (*QueryBuilder) Equals ¶
func (qb *QueryBuilder) Equals(key string, value any) *QueryBuilder
Equals adds an equality filter. Alias for Filter().
func (*QueryBuilder) Exists ¶
func (qb *QueryBuilder) Exists(key string, exists bool) *QueryBuilder
Exists adds an exists filter.
func (*QueryBuilder) Filter ¶
func (qb *QueryBuilder) Filter(key string, value any) *QueryBuilder
Filter adds a filter condition to the query.
func (*QueryBuilder) GetFilter ¶ added in v0.2.0
func (qb *QueryBuilder) GetFilter() bson.D
GetFilter returns the filter without options.
func (*QueryBuilder) GreaterThan ¶
func (qb *QueryBuilder) GreaterThan(key string, value any) *QueryBuilder
GreaterThan adds a greater than filter.
func (*QueryBuilder) GreaterThanOrEqual ¶
func (qb *QueryBuilder) GreaterThanOrEqual(key string, value any) *QueryBuilder
GreaterThanOrEqual adds a greater than or equal filter.
func (*QueryBuilder) In ¶
func (qb *QueryBuilder) In(key string, values ...any) *QueryBuilder
In adds an in filter.
func (*QueryBuilder) LessThan ¶
func (qb *QueryBuilder) LessThan(key string, value any) *QueryBuilder
LessThan adds a less than filter.
func (*QueryBuilder) LessThanOrEqual ¶
func (qb *QueryBuilder) LessThanOrEqual(key string, value any) *QueryBuilder
LessThanOrEqual adds a less than or equal filter.
func (*QueryBuilder) Limit ¶
func (qb *QueryBuilder) Limit(limit int64) *QueryBuilder
Limit sets the maximum number of documents to return.
func (*QueryBuilder) Nor ¶
func (qb *QueryBuilder) Nor(conditions ...bson.D) *QueryBuilder
Nor adds a nor condition.
func (*QueryBuilder) NorConditions ¶ added in v0.2.0
func (qb *QueryBuilder) NorConditions(builders ...*QueryBuilder) *QueryBuilder
NorConditions combines multiple QueryBuilders with $nor logic.
func (*QueryBuilder) NotEquals ¶
func (qb *QueryBuilder) NotEquals(key string, value any) *QueryBuilder
NotEquals adds a not equals filter.
func (*QueryBuilder) NotIn ¶
func (qb *QueryBuilder) NotIn(key string, values ...any) *QueryBuilder
NotIn adds a not in filter.
func (*QueryBuilder) Or ¶
func (qb *QueryBuilder) Or(conditions ...bson.D) *QueryBuilder
Or adds an or condition.
func (*QueryBuilder) OrConditions ¶ added in v0.2.0
func (qb *QueryBuilder) OrConditions(builders ...*QueryBuilder) *QueryBuilder
OrConditions combines multiple QueryBuilders with $or logic.
func (*QueryBuilder) Project ¶
func (qb *QueryBuilder) Project(projection any) *QueryBuilder
Project sets the projection.
func (*QueryBuilder) Regex ¶
func (qb *QueryBuilder) Regex(key string, pattern string, options string) *QueryBuilder
Regex adds a regex filter.
func (*QueryBuilder) Skip ¶
func (qb *QueryBuilder) Skip(skip int64) *QueryBuilder
Skip sets the number of documents to skip.
func (*QueryBuilder) Sort ¶
func (qb *QueryBuilder) Sort(field string, ascending bool) *QueryBuilder
Sort adds a field to the sort order.
func (*QueryBuilder) SortBy ¶
func (qb *QueryBuilder) SortBy(sort any) *QueryBuilder
SortBy sets custom sort order, replacing any previously set sort fields.
func (*QueryBuilder) Where ¶ added in v0.2.0
func (qb *QueryBuilder) Where(expression any) *QueryBuilder
Where adds a raw MongoDB expression to the filter.
type Repository ¶ added in v0.2.0
type Repository[T any] struct { // contains filtered or unexported fields }
Repository provides a type-safe, collection-specific interface for database operations.
func NewRepository ¶ added in v0.2.0
func NewRepository[T any](client *Client, collection string) *Repository[T]
NewRepository creates a new type-safe repository for the specified collection.
func (*Repository[T]) Aggregate ¶ added in v0.2.0
func (r *Repository[T]) Aggregate(ctx context.Context, pipeline any, opts ...*options.AggregateOptions) ([]T, error)
Aggregate executes an aggregation pipeline and returns typed results.
func (*Repository[T]) Collection ¶ added in v0.2.0
func (r *Repository[T]) Collection() string
Collection returns the name of the collection this repository operates on.
func (*Repository[T]) Count ¶ added in v0.2.0
func (r *Repository[T]) Count(ctx context.Context, filter any, opts ...*options.CountOptions) (int64, error)
Count returns the number of documents matching the filter.
func (*Repository[T]) CountAll ¶ added in v0.2.0
func (r *Repository[T]) CountAll(ctx context.Context, opts ...*options.CountOptions) (int64, error)
CountAll counts all documents in the collection.
func (*Repository[T]) CountWithBuilder ¶ added in v0.2.0
func (r *Repository[T]) CountWithBuilder(ctx context.Context, qb *QueryBuilder) (int64, error)
CountWithBuilder counts documents using a QueryBuilder filter.
func (*Repository[T]) Create ¶ added in v0.2.0
func (r *Repository[T]) Create(ctx context.Context, document T) (any, error)
Create inserts a new document and returns its ID.
func (*Repository[T]) CreateMany ¶ added in v0.2.0
func (r *Repository[T]) CreateMany(ctx context.Context, documents []T) ([]any, error)
CreateMany inserts multiple documents and returns their IDs.
func (*Repository[T]) DeleteByID ¶ added in v0.2.0
func (r *Repository[T]) DeleteByID(ctx context.Context, id any) (*mongo.DeleteResult, error)
DeleteByID deletes a single document by its _id field.
func (*Repository[T]) DeleteMany ¶ added in v0.2.0
func (r *Repository[T]) DeleteMany(ctx context.Context, filter any) (*mongo.DeleteResult, error)
DeleteMany deletes all documents matching the filter.
func (*Repository[T]) DeleteOne ¶ added in v0.2.0
func (r *Repository[T]) DeleteOne(ctx context.Context, filter any) (*mongo.DeleteResult, error)
DeleteOne deletes a single document matching the filter.
func (*Repository[T]) Drop ¶ added in v0.2.0
func (r *Repository[T]) Drop(ctx context.Context) error
Drop deletes the entire collection. WARNING: This permanently deletes all documents and indexes.
func (*Repository[T]) EstimatedCount ¶ added in v0.2.0
func (r *Repository[T]) EstimatedCount(ctx context.Context, opts ...*options.EstimatedDocumentCountOptions) (int64, error)
EstimatedCount returns an estimated count using collection metadata. Faster than Count but may be less accurate.
func (*Repository[T]) Exists ¶ added in v0.2.0
Exists checks if at least one document matching the filter exists.
func (*Repository[T]) ExistsByID ¶ added in v0.2.0
ExistsByID checks if a document with the given _id exists.
func (*Repository[T]) ExistsWithBuilder ¶ added in v0.2.0
func (r *Repository[T]) ExistsWithBuilder(ctx context.Context, qb *QueryBuilder) (bool, error)
ExistsWithBuilder checks if at least one document matching the QueryBuilder exists.
func (*Repository[T]) Find ¶ added in v0.2.0
func (r *Repository[T]) Find(ctx context.Context, filter any, opts ...*options.FindOptions) ([]T, error)
Find finds all documents matching the filter.
func (*Repository[T]) FindAll ¶ added in v0.2.0
func (r *Repository[T]) FindAll(ctx context.Context, opts ...*options.FindOptions) ([]T, error)
FindAll returns all documents in the collection.
func (*Repository[T]) FindByID ¶ added in v0.2.0
func (r *Repository[T]) FindByID(ctx context.Context, id any) (*T, error)
FindByID finds a single document by its _id field. Returns mongo.ErrNoDocuments if not found.
func (*Repository[T]) FindOne ¶ added in v0.2.0
func (r *Repository[T]) FindOne(ctx context.Context, filter any, opts ...*options.FindOneOptions) (*T, error)
FindOne finds a single document matching the filter. Returns mongo.ErrNoDocuments if not found.
func (*Repository[T]) FindOneWithBuilder ¶ added in v0.2.0
func (r *Repository[T]) FindOneWithBuilder(ctx context.Context, qb *QueryBuilder) (*T, error)
FindOneWithBuilder finds a single document using a QueryBuilder. Returns mongo.ErrNoDocuments if not found.
func (*Repository[T]) FindWithBuilder ¶ added in v0.2.0
func (r *Repository[T]) FindWithBuilder(ctx context.Context, qb *QueryBuilder) ([]T, error)
FindWithBuilder finds documents using a QueryBuilder for complex queries.
func (*Repository[T]) UpdateByID ¶ added in v0.2.0
func (r *Repository[T]) UpdateByID(ctx context.Context, id any, update any, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
UpdateByID updates a single document by its _id field.
func (*Repository[T]) UpdateMany ¶ added in v0.2.0
func (r *Repository[T]) UpdateMany(ctx context.Context, filter any, update any, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
UpdateMany updates all documents matching the filter.
func (*Repository[T]) UpdateOne ¶ added in v0.2.0
func (r *Repository[T]) UpdateOne(ctx context.Context, filter any, update any, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
UpdateOne updates a single document matching the filter.
func (*Repository[T]) Upsert ¶ added in v0.2.0
func (r *Repository[T]) Upsert(ctx context.Context, filter any, update any) (*mongo.UpdateResult, error)
Upsert updates a document if it exists, or inserts it if it doesn't.
type UpdateBuilder ¶
type UpdateBuilder struct {
// contains filtered or unexported fields
}
UpdateBuilder provides a fluent interface for building update operations
func NewUpdateBuilder ¶
func NewUpdateBuilder() *UpdateBuilder
NewUpdateBuilder creates a new UpdateBuilder instance.
func (*UpdateBuilder) AddToSet ¶
func (ub *UpdateBuilder) AddToSet(key string, value any) *UpdateBuilder
AddToSet adds value to array if not already present.
func (*UpdateBuilder) Build ¶
func (ub *UpdateBuilder) Build() bson.D
Build returns the update document.
func (*UpdateBuilder) CurrentDate ¶
func (ub *UpdateBuilder) CurrentDate(key string) *UpdateBuilder
CurrentDate sets field to current date.
func (*UpdateBuilder) Inc ¶
func (ub *UpdateBuilder) Inc(key string, value any) *UpdateBuilder
Inc increments field values.
func (*UpdateBuilder) Max ¶
func (ub *UpdateBuilder) Max(key string, value any) *UpdateBuilder
Max updates field if specified value is greater than current value.
func (*UpdateBuilder) Min ¶
func (ub *UpdateBuilder) Min(key string, value any) *UpdateBuilder
Min updates field if specified value is less than current value.
func (*UpdateBuilder) Mul ¶
func (ub *UpdateBuilder) Mul(key string, value any) *UpdateBuilder
Mul multiplies field values.
func (*UpdateBuilder) Pop ¶
func (ub *UpdateBuilder) Pop(key string, first bool) *UpdateBuilder
Pop removes first or last element from array.
func (*UpdateBuilder) Pull ¶
func (ub *UpdateBuilder) Pull(key string, value any) *UpdateBuilder
Pull removes all instances of value from array.
func (*UpdateBuilder) Push ¶
func (ub *UpdateBuilder) Push(key string, value any) *UpdateBuilder
Push appends value to array.
func (*UpdateBuilder) Rename ¶
func (ub *UpdateBuilder) Rename(oldName string, newName string) *UpdateBuilder
Rename renames a field.
func (*UpdateBuilder) Set ¶
func (ub *UpdateBuilder) Set(key string, value any) *UpdateBuilder
Set sets field values.
func (*UpdateBuilder) Unset ¶
func (ub *UpdateBuilder) Unset(keys ...string) *UpdateBuilder
Unset removes fields.
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
aggregations
command
|
|
|
basic_crud
command
|
|
|
query_builders
command
|
|
|
update_builders
command
|
|