Documentation
¶
Index ¶
- Variables
- func NewConnection(cfg *config.DatabaseConfig, log logger.Logger) (database.Interface, error)
- type Builder
- func (b *Builder) AddGroup(group bson.M) *Builder
- func (b *Builder) AddLimit(limit int64) *Builder
- func (b *Builder) AddLookup(from, localField, foreignField, as string) *Builder
- func (b *Builder) AddMatch(filter bson.M) *Builder
- func (b *Builder) AddProject(projection bson.M) *Builder
- func (b *Builder) AddSkip(skip int64) *Builder
- func (b *Builder) AddSort(sort bson.D) *Builder
- func (b *Builder) AddStage(stage bson.M) *Builder
- func (b *Builder) AddUnwind(field string) *Builder
- func (b *Builder) Clone() *Builder
- func (b *Builder) Exclude(fields ...string) *Builder
- func (b *Builder) GetLimit() *int64
- func (b *Builder) GetProjectionFields() []string
- func (b *Builder) GetSkip() *int64
- func (b *Builder) GetSortFields() []string
- func (b *Builder) GetState() BuilderState
- func (b *Builder) HasFilter() bool
- func (b *Builder) HasPagination() bool
- func (b *Builder) HasProjection() bool
- func (b *Builder) HasSort() bool
- func (b *Builder) Limit(count int64) *Builder
- func (b *Builder) Offset(count int64) *Builder
- func (b *Builder) OrderBy(field string) *Builder
- func (b *Builder) OrderByAsc(field string) *Builder
- func (b *Builder) OrderByDesc(field string) *Builder
- func (b *Builder) ProjectElemMatch(field string, condition any) *Builder
- func (b *Builder) ProjectSlice(field string, limit int) *Builder
- func (b *Builder) ProjectSliceWithSkip(field string, skip, limit int) *Builder
- func (b *Builder) Reset() *Builder
- func (b *Builder) Select(fields ...string) *Builder
- func (b *Builder) Skip(count int64) *Builder
- func (b *Builder) String() string
- func (b *Builder) ToFilter() bson.M
- func (b *Builder) ToFindOptions() *FindOptionsBuilder
- func (b *Builder) ToJSON() string
- func (b *Builder) ToPipeline() []bson.M
- func (b *Builder) ToUpdateDocument(update bson.M) bson.M
- func (b *Builder) Where(field string, value any) *Builder
- func (b *Builder) WhereAnd(filters ...bson.M) *Builder
- func (b *Builder) WhereEq(field string, value any) *Builder
- func (b *Builder) WhereExists(field string, exists bool) *Builder
- func (b *Builder) WhereGt(field string, value any) *Builder
- func (b *Builder) WhereGte(field string, value any) *Builder
- func (b *Builder) WhereIn(field string, values ...any) *Builder
- func (b *Builder) WhereLt(field string, value any) *Builder
- func (b *Builder) WhereLte(field string, value any) *Builder
- func (b *Builder) WhereNe(field string, value any) *Builder
- func (b *Builder) WhereNin(field string, values ...any) *Builder
- func (b *Builder) WhereNor(filters ...bson.M) *Builder
- func (b *Builder) WhereNot(filter bson.M) *Builder
- func (b *Builder) WhereOr(filters ...bson.M) *Builder
- func (b *Builder) WhereRegex(field, pattern, options string) *Builder
- func (b *Builder) WhereType(field string, bsonType any) *Builder
- func (b *Builder) WithOptions(_ BuilderOptions) *Builder
- type BuilderOptions
- type BuilderState
- type BulkWriteResult
- type ChangeStreamWrapper
- func (c *ChangeStreamWrapper) Close(ctx context.Context) error
- func (c *ChangeStreamWrapper) Decode(val any) error
- func (c *ChangeStreamWrapper) Err() error
- func (c *ChangeStreamWrapper) Next(ctx context.Context) bool
- func (c *ChangeStreamWrapper) ResumeToken() bson.Raw
- func (c *ChangeStreamWrapper) TryNext(ctx context.Context) bool
- type Collection
- func (c *Collection) Aggregate(ctx context.Context, pipeline any, opts *database.AggregateOptions) (database.DocumentCursor, error)
- func (c *Collection) BulkWrite(ctx context.Context, models []database.WriteModel, ...) (database.DocumentBulkWriteResult, error)
- func (c *Collection) CountDocuments(ctx context.Context, filter any, opts *database.CountOptions) (int64, error)
- func (c *Collection) CreateIndex(ctx context.Context, model database.IndexModel) error
- func (c *Collection) CreateIndexes(ctx context.Context, models []database.IndexModel) error
- func (c *Collection) DeleteMany(ctx context.Context, filter any, _ *database.DeleteOptions) (database.DocumentDeleteResult, error)
- func (c *Collection) DeleteOne(ctx context.Context, filter any, _ *database.DeleteOptions) (database.DocumentDeleteResult, error)
- func (c *Collection) Distinct(ctx context.Context, fieldName string, filter any, ...) ([]any, error)
- func (c *Collection) DropIndex(ctx context.Context, name string) error
- func (c *Collection) EstimatedDocumentCount(ctx context.Context, opts *database.EstimatedCountOptions) (int64, error)
- func (c *Collection) Find(ctx context.Context, filter any, opts *database.FindOptions) (database.DocumentCursor, error)
- func (c *Collection) FindOne(ctx context.Context, filter any, opts *database.FindOneOptions) database.DocumentResult
- func (c *Collection) InsertMany(ctx context.Context, documents []any, opts *database.InsertManyOptions) ([]any, error)
- func (c *Collection) InsertOne(ctx context.Context, document any, opts *database.InsertOneOptions) (any, error)
- func (c *Collection) ListIndexes(ctx context.Context) (database.DocumentCursor, error)
- func (c *Collection) ReplaceOne(ctx context.Context, filter, replacement any, opts *database.ReplaceOptions) (database.DocumentUpdateResult, error)
- func (c *Collection) UpdateMany(ctx context.Context, filter, update any, opts *database.UpdateOptions) (database.DocumentUpdateResult, error)
- func (c *Collection) UpdateOne(ctx context.Context, filter, update any, opts *database.UpdateOptions) (database.DocumentUpdateResult, error)
- func (c *Collection) Watch(ctx context.Context, pipeline any, opts *database.ChangeStreamOptions) (database.ChangeStream, error)
- type Connection
- func (c *Connection) Aggregate(ctx context.Context, collection string, pipeline any, ...) (database.DocumentCursor, error)
- func (c *Connection) Begin(ctx context.Context) (database.Tx, error)
- func (c *Connection) BeginTx(ctx context.Context, opts *sql.TxOptions) (database.Tx, error)
- func (c *Connection) Close() error
- func (c *Connection) Collection(name string) database.DocumentCollection
- func (c *Connection) CreateCollection(ctx context.Context, name string, opts *database.CreateCollectionOptions) error
- func (c *Connection) CreateIndex(ctx context.Context, collection string, model database.IndexModel) error
- func (c *Connection) CreateMigrationTable(ctx context.Context) error
- func (c *Connection) DatabaseType() string
- func (c *Connection) DropCollection(ctx context.Context, name string) error
- func (c *Connection) DropIndex(ctx context.Context, collection, name string) error
- func (c *Connection) Exec(_ context.Context, _ string, _ ...any) (sql.Result, error)
- func (c *Connection) GetClient() *mongo.Client
- func (c *Connection) GetDatabase() *mongo.Database
- func (c *Connection) GetMigrationTable() string
- func (c *Connection) Health(ctx context.Context) error
- func (c *Connection) ListIndexes(ctx context.Context, collection string) ([]database.IndexModel, error)
- func (c *Connection) Prepare(_ context.Context, _ string) (database.Statement, error)
- func (c *Connection) Query(_ context.Context, _ string, _ ...any) (*sql.Rows, error)
- func (c *Connection) QueryRow(_ context.Context, _ string, _ ...any) types.Row
- func (c *Connection) RunCommand(ctx context.Context, command any) database.DocumentResult
- func (c *Connection) Stats() (map[string]any, error)
- type ConnectionBuilder
- type Cursor
- func (c *Cursor) All(ctx context.Context, results any) error
- func (c *Cursor) Close(ctx context.Context) error
- func (c *Cursor) Current() bson.Raw
- func (c *Cursor) Decode(val any) error
- func (c *Cursor) Err() error
- func (c *Cursor) ID() int64
- func (c *Cursor) Next(ctx context.Context) bool
- func (c *Cursor) TryNext(ctx context.Context) bool
- type DeleteManyModel
- type DeleteOneModel
- type DeleteResult
- type FindOptionsBuilder
- func (f *FindOptionsBuilder) Build() *database.FindOptions
- func (f *FindOptionsBuilder) Limit(limit int64) *FindOptionsBuilder
- func (f *FindOptionsBuilder) Projection(projection any) *FindOptionsBuilder
- func (f *FindOptionsBuilder) Skip(skip int64) *FindOptionsBuilder
- func (f *FindOptionsBuilder) Sort(sort any) *FindOptionsBuilder
- type InsertOneModel
- type ReplaceOneModel
- type SingleResult
- type Transaction
- func (t *Transaction) Commit() error
- func (t *Transaction) Exec(_ context.Context, _ string, _ ...any) (sql.Result, error)
- func (t *Transaction) GetDatabase() *mongo.Database
- func (t *Transaction) GetSession() mongo.Session
- func (t *Transaction) Prepare(_ context.Context, _ string) (database.Statement, error)
- func (t *Transaction) Query(_ context.Context, _ string, _ ...any) (*sql.Rows, error)
- func (t *Transaction) QueryRow(_ context.Context, _ string, _ ...any) types.Row
- func (t *Transaction) Rollback() error
- type UpdateManyModel
- type UpdateOneModel
- type UpdateResult
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidReadPreference = errors.New("invalid read preference") ErrInvalidWriteConcern = errors.New("invalid write concern") )
Sentinel errors for MongoDB configuration validation
Functions ¶
func NewConnection ¶
NewConnection creates a new MongoDB connection
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder provides a fluent interface for building MongoDB queries and aggregation pipelines This follows the Squirrel pattern but for MongoDB's document-oriented operations.
func (*Builder) AddProject ¶
AddProject adds a $project stage
func (*Builder) GetProjectionFields ¶
GetProjectionFields returns the fields being projected
func (*Builder) GetSortFields ¶
GetSortFields returns the fields being sorted
func (*Builder) GetState ¶
func (b *Builder) GetState() BuilderState
GetState returns the current builder state for testing purposes
func (*Builder) HasPagination ¶
HasPagination returns true if the builder has skip or limit set
func (*Builder) HasProjection ¶
HasProjection returns true if the builder has projection settings
func (*Builder) OrderByAsc ¶
OrderByAsc adds a sort field in ascending order (alias for OrderBy)
func (*Builder) OrderByDesc ¶
OrderByDesc adds a sort field in descending order
func (*Builder) ProjectElemMatch ¶
ProjectElemMatch projects elements that match a condition
func (*Builder) ProjectSlice ¶
ProjectSlice projects an array slice
func (*Builder) ProjectSliceWithSkip ¶
ProjectSliceWithSkip projects an array slice with skip
func (*Builder) ToFindOptions ¶
func (b *Builder) ToFindOptions() *FindOptionsBuilder
ToFindOptions builds options for find operations
func (*Builder) ToPipeline ¶
ToPipeline builds an aggregation pipeline
func (*Builder) ToUpdateDocument ¶
ToUpdateDocument builds an update document
func (*Builder) WhereExists ¶
WhereExists adds an existence check condition
func (*Builder) WhereRegex ¶
WhereRegex adds a regex pattern condition
func (*Builder) WithOptions ¶
func (b *Builder) WithOptions(_ BuilderOptions) *Builder
WithOptions configures builder behavior
type BuilderOptions ¶
type BuilderOptions struct {
PipelineOrder []string // Order of pipeline stages: "match", "sort", "skip", "limit", "project"
}
BuilderOptions configures Builder behavior
type BuilderState ¶
type BuilderState struct { Match bson.M Sort bson.D Skip *int64 Limit *int64 Projection bson.M Pipeline []bson.M }
BuilderState represents the internal state of a Builder for testing
type BulkWriteResult ¶
type BulkWriteResult struct {
// contains filtered or unexported fields
}
BulkWriteResult wraps mongo.BulkWriteResult to implement DocumentBulkWriteResult interface
func (*BulkWriteResult) DeletedCount ¶
func (r *BulkWriteResult) DeletedCount() int64
func (*BulkWriteResult) InsertedCount ¶
func (r *BulkWriteResult) InsertedCount() int64
func (*BulkWriteResult) MatchedCount ¶
func (r *BulkWriteResult) MatchedCount() int64
func (*BulkWriteResult) ModifiedCount ¶
func (r *BulkWriteResult) ModifiedCount() int64
func (*BulkWriteResult) UpsertedCount ¶
func (r *BulkWriteResult) UpsertedCount() int64
func (*BulkWriteResult) UpsertedIDs ¶
func (r *BulkWriteResult) UpsertedIDs() map[int64]any
type ChangeStreamWrapper ¶
type ChangeStreamWrapper struct {
// contains filtered or unexported fields
}
ChangeStreamWrapper wraps mongo.ChangeStream to implement ChangeStream interface
func (*ChangeStreamWrapper) Decode ¶
func (c *ChangeStreamWrapper) Decode(val any) error
func (*ChangeStreamWrapper) Err ¶
func (c *ChangeStreamWrapper) Err() error
func (*ChangeStreamWrapper) ResumeToken ¶
func (c *ChangeStreamWrapper) ResumeToken() bson.Raw
type Collection ¶
type Collection struct {
// contains filtered or unexported fields
}
Collection wraps a MongoDB collection to implement DocumentCollection interface
func (*Collection) Aggregate ¶
func (c *Collection) Aggregate(ctx context.Context, pipeline any, opts *database.AggregateOptions) (database.DocumentCursor, error)
Aggregate performs aggregation on the collection
func (*Collection) BulkWrite ¶
func (c *Collection) BulkWrite(ctx context.Context, models []database.WriteModel, opts *database.BulkWriteOptions) (database.DocumentBulkWriteResult, error)
BulkWrite performs bulk write operations
func (*Collection) CountDocuments ¶
func (c *Collection) CountDocuments(ctx context.Context, filter any, opts *database.CountOptions) (int64, error)
CountDocuments counts documents matching the filter
func (*Collection) CreateIndex ¶
func (c *Collection) CreateIndex(ctx context.Context, model database.IndexModel) error
CreateIndex creates an index on the collection
func (*Collection) CreateIndexes ¶
func (c *Collection) CreateIndexes(ctx context.Context, models []database.IndexModel) error
CreateIndexes creates multiple indexes on the collection
func (*Collection) DeleteMany ¶
func (c *Collection) DeleteMany(ctx context.Context, filter any, _ *database.DeleteOptions) (database.DocumentDeleteResult, error)
DeleteMany deletes multiple documents
func (*Collection) DeleteOne ¶
func (c *Collection) DeleteOne(ctx context.Context, filter any, _ *database.DeleteOptions) (database.DocumentDeleteResult, error)
DeleteOne deletes a single document
func (*Collection) Distinct ¶
func (c *Collection) Distinct(ctx context.Context, fieldName string, filter any, opts *database.DistinctOptions) ([]any, error)
Distinct returns distinct values for a field
func (*Collection) DropIndex ¶
func (c *Collection) DropIndex(ctx context.Context, name string) error
DropIndex drops an index from the collection
func (*Collection) EstimatedDocumentCount ¶
func (c *Collection) EstimatedDocumentCount(ctx context.Context, opts *database.EstimatedCountOptions) (int64, error)
EstimatedDocumentCount returns an estimated count of documents in the collection
func (*Collection) Find ¶
func (c *Collection) Find(ctx context.Context, filter any, opts *database.FindOptions) (database.DocumentCursor, error)
Find finds multiple documents
func (*Collection) FindOne ¶
func (c *Collection) FindOne(ctx context.Context, filter any, opts *database.FindOneOptions) database.DocumentResult
FindOne finds a single document
func (*Collection) InsertMany ¶
func (c *Collection) InsertMany(ctx context.Context, documents []any, opts *database.InsertManyOptions) ([]any, error)
InsertMany inserts multiple documents
func (*Collection) InsertOne ¶
func (c *Collection) InsertOne(ctx context.Context, document any, opts *database.InsertOneOptions) (any, error)
InsertOne inserts a single document
func (*Collection) ListIndexes ¶
func (c *Collection) ListIndexes(ctx context.Context) (database.DocumentCursor, error)
ListIndexes lists all indexes on the collection
func (*Collection) ReplaceOne ¶
func (c *Collection) ReplaceOne(ctx context.Context, filter, replacement any, opts *database.ReplaceOptions) (database.DocumentUpdateResult, error)
ReplaceOne replaces a single document
func (*Collection) UpdateMany ¶
func (c *Collection) UpdateMany(ctx context.Context, filter, update any, opts *database.UpdateOptions) (database.DocumentUpdateResult, error)
UpdateMany updates multiple documents
func (*Collection) UpdateOne ¶
func (c *Collection) UpdateOne(ctx context.Context, filter, update any, opts *database.UpdateOptions) (database.DocumentUpdateResult, error)
UpdateOne updates a single document
func (*Collection) Watch ¶
func (c *Collection) Watch(ctx context.Context, pipeline any, opts *database.ChangeStreamOptions) (database.ChangeStream, error)
Watch creates a change stream for the collection
type Connection ¶
type Connection struct {
// contains filtered or unexported fields
}
Connection implements the database.Interface for MongoDB
func (*Connection) Aggregate ¶
func (c *Connection) Aggregate(ctx context.Context, collection string, pipeline any, opts *database.AggregateOptions) (database.DocumentCursor, error)
Aggregate performs aggregation on the specified collection
func (*Connection) Collection ¶
func (c *Connection) Collection(name string) database.DocumentCollection
Collection returns a DocumentCollection for the specified collection name
func (*Connection) CreateCollection ¶
func (c *Connection) CreateCollection(ctx context.Context, name string, opts *database.CreateCollectionOptions) error
CreateCollection creates a new collection with the specified options
func (*Connection) CreateIndex ¶
func (c *Connection) CreateIndex(ctx context.Context, collection string, model database.IndexModel) error
CreateIndex creates an index on the specified collection
func (*Connection) CreateMigrationTable ¶
func (c *Connection) CreateMigrationTable(ctx context.Context) error
CreateMigrationTable creates the migration collection if it doesn't exist
func (*Connection) DatabaseType ¶
func (c *Connection) DatabaseType() string
DatabaseType returns the database type
func (*Connection) DropCollection ¶
func (c *Connection) DropCollection(ctx context.Context, name string) error
DropCollection drops the specified collection
func (*Connection) DropIndex ¶
func (c *Connection) DropIndex(ctx context.Context, collection, name string) error
DropIndex drops an index from the specified collection
func (*Connection) GetClient ¶
func (c *Connection) GetClient() *mongo.Client
GetClient returns the underlying MongoDB client instance
func (*Connection) GetDatabase ¶
func (c *Connection) GetDatabase() *mongo.Database
GetDatabase returns the underlying MongoDB database instance
func (*Connection) GetMigrationTable ¶
func (c *Connection) GetMigrationTable() string
GetMigrationTable returns the migration collection name for MongoDB
func (*Connection) Health ¶
func (c *Connection) Health(ctx context.Context) error
Health checks MongoDB connection health
func (*Connection) ListIndexes ¶
func (c *Connection) ListIndexes(ctx context.Context, collection string) ([]database.IndexModel, error)
ListIndexes lists all indexes on the specified collection
func (*Connection) QueryRow ¶
QueryRow executes a MongoDB query returning a single row (not applicable)
func (*Connection) RunCommand ¶
func (c *Connection) RunCommand(ctx context.Context, command any) database.DocumentResult
RunCommand executes a database command
type ConnectionBuilder ¶
type ConnectionBuilder struct {
// contains filtered or unexported fields
}
ConnectionBuilder helps build MongoDB client options step by step
type Cursor ¶
type Cursor struct {
// contains filtered or unexported fields
}
Cursor wraps mongo.Cursor to implement DocumentCursor interface
type DeleteManyModel ¶
type DeleteManyModel struct {
Filter any
}
func (*DeleteManyModel) GetModel ¶
func (m *DeleteManyModel) GetModel() any
type DeleteOneModel ¶
type DeleteOneModel struct {
Filter any
}
func (*DeleteOneModel) GetModel ¶
func (m *DeleteOneModel) GetModel() any
type DeleteResult ¶
type DeleteResult struct {
// contains filtered or unexported fields
}
DeleteResult wraps mongo.DeleteResult to implement DocumentDeleteResult interface
func (*DeleteResult) DeletedCount ¶
func (r *DeleteResult) DeletedCount() int64
type FindOptionsBuilder ¶
type FindOptionsBuilder struct {
// contains filtered or unexported fields
}
FindOptionsBuilder helps build find options
func (*FindOptionsBuilder) Build ¶
func (f *FindOptionsBuilder) Build() *database.FindOptions
Build builds the find options for the database package
func (*FindOptionsBuilder) Limit ¶
func (f *FindOptionsBuilder) Limit(limit int64) *FindOptionsBuilder
Limit sets the limit option
func (*FindOptionsBuilder) Projection ¶
func (f *FindOptionsBuilder) Projection(projection any) *FindOptionsBuilder
Projection sets the projection option
func (*FindOptionsBuilder) Skip ¶
func (f *FindOptionsBuilder) Skip(skip int64) *FindOptionsBuilder
Skip sets the skip option
func (*FindOptionsBuilder) Sort ¶
func (f *FindOptionsBuilder) Sort(sort any) *FindOptionsBuilder
Sort sets the sort option
type InsertOneModel ¶
type InsertOneModel struct {
Document any
}
WriteModel implementations for common write operations
func (*InsertOneModel) GetModel ¶
func (m *InsertOneModel) GetModel() any
type ReplaceOneModel ¶
func (*ReplaceOneModel) GetModel ¶
func (m *ReplaceOneModel) GetModel() any
type SingleResult ¶
type SingleResult struct {
// contains filtered or unexported fields
}
SingleResult wraps mongo.SingleResult to implement DocumentResult interface
func (*SingleResult) Decode ¶
func (r *SingleResult) Decode(v any) error
func (*SingleResult) Err ¶
func (r *SingleResult) Err() error
type Transaction ¶
type Transaction struct {
// contains filtered or unexported fields
}
Transaction implements the database.Tx interface for MongoDB
func (*Transaction) Commit ¶
func (t *Transaction) Commit() error
Commit commits the MongoDB transaction
func (*Transaction) Exec ¶
Exec executes a command within the transaction (not applicable for MongoDB)
func (*Transaction) GetDatabase ¶
func (t *Transaction) GetDatabase() *mongo.Database
GetDatabase returns the database instance for use within the transaction
func (*Transaction) GetSession ¶
func (t *Transaction) GetSession() mongo.Session
GetSession returns the underlying MongoDB session for document operations
func (*Transaction) Prepare ¶
Prepare creates a prepared statement within the transaction (not applicable)
func (*Transaction) Query ¶
Query executes a query within the transaction (not applicable for MongoDB)
func (*Transaction) QueryRow ¶
QueryRow executes a query returning a single row within the transaction (not applicable)
func (*Transaction) Rollback ¶
func (t *Transaction) Rollback() error
Rollback rolls back the MongoDB transaction
type UpdateManyModel ¶
func (*UpdateManyModel) GetModel ¶
func (m *UpdateManyModel) GetModel() any
type UpdateOneModel ¶
func (*UpdateOneModel) GetModel ¶
func (m *UpdateOneModel) GetModel() any
type UpdateResult ¶
type UpdateResult struct {
// contains filtered or unexported fields
}
UpdateResult wraps mongo.UpdateResult to implement DocumentUpdateResult interface
func (*UpdateResult) MatchedCount ¶
func (r *UpdateResult) MatchedCount() int64
func (*UpdateResult) ModifiedCount ¶
func (r *UpdateResult) ModifiedCount() int64
func (*UpdateResult) UpsertedCount ¶
func (r *UpdateResult) UpsertedCount() int64
func (*UpdateResult) UpsertedID ¶
func (r *UpdateResult) UpsertedID() any