Documentation ¶
Index ¶
- func AddIndex(ctx context.Context, collName string, index mongo.IndexModel, ...) (string, error)
- func AddIndexT[T Modeler](ctx context.Context, index mongo.IndexModel, ...) (string, error)
- func AddIndexes(ctx context.Context, collName string, indexes []mongo.IndexModel, ...) ([]string, error)
- func AddIndexesT[T Modeler](ctx context.Context, indexes []mongo.IndexModel, ...) ([]string, error)
- func AddOrUpdate[T Modeler](ctx context.Context, filter interface{}, model T) (*mongo.UpdateResult, error)
- func Aggregate(ctx context.Context, collName string, req *AggregationRequest) (res []bson.M, err error)
- func AggregateT[T Modeler](ctx context.Context, req *AggregationRequest) (res []bson.M, err error)
- func CollRead(collName string) *mongo.Collection
- func CollWrite(collName string) *mongo.Collection
- func Connect(ctx context.Context, uri string, dbName string) error
- func Count(ctx context.Context, collName string, filter interface{}, ...) (int64, error)
- func CountT[T Modeler](ctx context.Context, filter interface{}, opts ...*options.CountOptions) (int64, error)
- func Create(ctx context.Context, collName string, model any, ...) (interface{}, error)
- func CreateCollection[T Modeler](ctx context.Context, opts ...*options.CreateCollectionOptions) error
- func CreateMany(ctx context.Context, collName string, models []any, ...) (*mongo.InsertManyResult, error)
- func CreateManyT[T Modeler](ctx context.Context, models []T, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error)
- func CreateT[T Modeler](ctx context.Context, model T, opts ...*options.InsertOneOptions) (interface{}, error)
- func CreateWithID[T IDModeler](ctx context.Context, model T, opts ...*options.InsertOneOptions) error
- func Delete(ctx context.Context, collName string, filter interface{}, ...) (*mongo.DeleteResult, error)
- func DeleteOne(ctx context.Context, collName string, filter interface{}, ...) (*mongo.DeleteResult, error)
- func DeleteOneT[T Modeler](ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
- func DeleteT[T Modeler](ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
- func EstimatedCount(ctx context.Context, collName string, ...) (int64, error)
- func EstimatedCountT[T Modeler](ctx context.Context, opts ...*options.EstimatedDocumentCountOptions) (int64, error)
- func FindOne[T Modeler](ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) (res *T, err error)
- func FindT[T Modeler](ctx context.Context, filter interface{}, opts ...*options.FindOptions) (res []T, err error)
- func Flush[T Modeler](ctx context.Context) (int64, error)
- func GetClient() *mongo.Client
- func ReplaceOne(ctx context.Context, collName string, filter interface{}, model any, ...) (*mongo.UpdateResult, error)
- func ReplaceOneT[T Modeler](ctx context.Context, filter interface{}, model T, ...) (*mongo.UpdateResult, error)
- func SetDatabase(database *mongo.Database)
- func Tx(ctx context.Context, cfg TransactionConfig) (interface{}, error)
- func TxOptimal(ctx context.Context, f func(ctx mongo.SessionContext) (interface{}, error)) (interface{}, error)
- func UpdateAndReturn[T Modeler](ctx context.Context, filter interface{}, update interface{}, ...) (*T, error)
- func UpdateMany(ctx context.Context, collName string, filter interface{}, update interface{}, ...) (*mongo.UpdateResult, error)
- func UpdateManyT[T Modeler](ctx context.Context, filter interface{}, update interface{}, ...) (*mongo.UpdateResult, error)
- func UpdateOne(ctx context.Context, collName string, filter interface{}, update interface{}, ...) (*mongo.UpdateResult, error)
- func UpdateOneT[T Modeler](ctx context.Context, filter interface{}, update interface{}, ...) (*mongo.UpdateResult, error)
- type AggregationRequest
- type Database
- type ID
- type IDModeler
- type IDT
- type IDer
- type IntID
- type Modeler
- type ObjectID
- type SessionKey
- type StringID
- type TransactionConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddIndex ¶
func AddIndex(ctx context.Context, collName string, index mongo.IndexModel, opts ...*options.CreateIndexesOptions) (string, error)
AddIndex adds an index to collection
func AddIndexT ¶
func AddIndexT[T Modeler](ctx context.Context, index mongo.IndexModel, opts ...*options.CreateIndexesOptions) (string, error)
AddIndex adds an index to collection model should be implement `CollName() string`
func AddIndexes ¶
func AddIndexes(ctx context.Context, collName string, indexes []mongo.IndexModel, opts ...*options.CreateIndexesOptions) ([]string, error)
AddIndexes adds indexes to collection
func AddIndexesT ¶
func AddIndexesT[T Modeler](ctx context.Context, indexes []mongo.IndexModel, opts ...*options.CreateIndexesOptions) ([]string, error)
AddIndexes adds indexes to collection model should be implement `CollName() string`
func AddOrUpdate ¶
func AddOrUpdate[T Modeler](ctx context.Context, filter interface{}, model T) (*mongo.UpdateResult, error)
AddOrUpdate adds or updates one document from collection model should be implement `CollName() string`
func Aggregate ¶
func Aggregate(ctx context.Context, collName string, req *AggregationRequest) (res []bson.M, err error)
Aggregate aggregates documents based on Pipeline and Options from request
func AggregateT ¶
Aggregate aggregates documents based on Pipeline and Options from request model should be implement `CollName() string`
func CollRead ¶
func CollRead(collName string) *mongo.Collection
func CollWrite ¶
func CollWrite(collName string) *mongo.Collection
func Count ¶
func Count(ctx context.Context, collName string, filter interface{}, opts ...*options.CountOptions) (int64, error)
Count counts documents from collection
func CountT ¶
func CountT[T Modeler](ctx context.Context, filter interface{}, opts ...*options.CountOptions) (int64, error)
CountT counts documents from collection model should be implement `CollName() string`
func CreateCollection ¶
func CreateMany ¶
func CreateMany(ctx context.Context, collName string, models []any, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error)
func CreateManyT ¶
func CreateManyT[T Modeler](ctx context.Context, models []T, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error)
func CreateWithID ¶
func Delete ¶
func Delete(ctx context.Context, collName string, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
Delete deletes many documents from collection
func DeleteOne ¶
func DeleteOne(ctx context.Context, collName string, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
DeleteOne deletes one document from collection
func DeleteOneT ¶
func DeleteOneT[T Modeler](ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
DeleteOne deletes one document from collection
func DeleteT ¶
func DeleteT[T Modeler](ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
Delete deletes many documents from collection
func EstimatedCount ¶
func EstimatedCount(ctx context.Context, collName string, opts ...*options.EstimatedDocumentCountOptions) (int64, error)
EstimatedCount counts documents from collection but it is not accurate, still faster than Count
func EstimatedCountT ¶
func EstimatedCountT[T Modeler](ctx context.Context, opts ...*options.EstimatedDocumentCountOptions) (int64, error)
EstimatedCountT counts documents from collection but it is not accurate, still faster than Count model should be implement `CollName() string`
func FindOne ¶
func FindOne[T Modeler](ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) (res *T, err error)
FindOne finds one document from model's collection model should be implement `CollName() string`
func FindT ¶
func FindT[T Modeler](ctx context.Context, filter interface{}, opts ...*options.FindOptions) (res []T, err error)
Find finds documents from from model's collection model should be implement `CollName() string`
func ReplaceOne ¶
func ReplaceOne(ctx context.Context, collName string, filter interface{}, model any, opts ...*options.ReplaceOptions) (*mongo.UpdateResult, error)
ReplaceOne replaces one document from collection upsert = true if you want to upsert or just use mocom.AddOrUpdate
func ReplaceOneT ¶
func ReplaceOneT[T Modeler](ctx context.Context, filter interface{}, model T, opts ...*options.ReplaceOptions) (*mongo.UpdateResult, error)
ReplaceOne replaces one document from collection upsert = true if you want to upsert, or just use mocom.AddOrUpdate model should be implement `CollName() string`
func SetDatabase ¶
func Tx ¶
func Tx(ctx context.Context, cfg TransactionConfig) (interface{}, error)
Tx executes a MongoDB transaction based on provided configuration (cfg). ctx is the context for the transaction, while cfg includes session configuration, transaction options, and the transaction function. Returns the result of transaction function execution or an error if the client is nil or any issues occurred during the session creation or transaction. If a nested transaction is detected, then this transaction will be executed with the passed-in context.
func TxOptimal ¶
func TxOptimal(ctx context.Context, f func(ctx mongo.SessionContext) (interface{}, error)) (interface{}, error)
TxOptimal will do the transaction with majority write-concern snapshot read-concern, primary read preference
This should be used when transaction does not contain any read If a nested transaction is detected, then this transaction will be executed with the passed-in context.
func UpdateAndReturn ¶
func UpdateAndReturn[T Modeler](ctx context.Context, filter interface{}, update interface{}, opts ...*options.FindOneAndUpdateOptions) (*T, error)
UpdateAndReturn updates one document from collection and return the updated document Pass options to return document AFTER or BEFORE the update
func UpdateMany ¶
func UpdateMany(ctx context.Context, collName string, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
func UpdateManyT ¶
func UpdateManyT[T Modeler](ctx context.Context, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
UpdateMany updates many documents from collection model should be implement `CollName() string`
func UpdateOne ¶
func UpdateOne(ctx context.Context, collName string, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
func UpdateOneT ¶
func UpdateOneT[T Modeler](ctx context.Context, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
UpdateOne updates one document from collection
Types ¶
type AggregationRequest ¶
type AggregationRequest struct { Pipeline []moper.D Options []*options.AggregateOptions }
-------- AGGREGATION --------
type SessionKey ¶
type SessionKey struct{}
type TransactionConfig ¶
type TransactionConfig struct { Options *options.TransactionOptions SessConfig *options.SessionOptions Func func(ctx mongo.SessionContext) (interface{}, error) }
-------- TRANSACTION -------- TransactionConfig, you only need to define the func; the transaction options are not necessary to be defined for simple use cases