Documentation
¶
Index ¶
- Constants
- func Aggregate[T any](ctx context.Context, coll *mongo.Collection, pipeline any, ...) ([]T, error)
- func Check(ctx context.Context, config *Config) error
- func Count(ctx context.Context, coll *mongo.Collection, filter any, ...) (int64, error)
- func DecodeOne[T any](decoder func(v any) error) (res T, err error)
- func Decodes[T any](ctx context.Context, cur *mongo.Cursor) ([]T, error)
- func DeleteMany(ctx context.Context, coll *mongo.Collection, filter any, ...) error
- func DeleteManyResult(ctx context.Context, coll *mongo.Collection, filter any, ...) (*mongo.DeleteResult, error)
- func DeleteOne(ctx context.Context, coll *mongo.Collection, filter any, ...) error
- func DeleteOneResult(ctx context.Context, coll *mongo.Collection, filter any, ...) (*mongo.DeleteResult, error)
- func Exist(ctx context.Context, coll *mongo.Collection, filter any, ...) (bool, error)
- func Find[T any](ctx context.Context, coll *mongo.Collection, filter any, ...) ([]T, error)
- func FindOne[T any](ctx context.Context, coll *mongo.Collection, filter any, ...) (res T, err error)
- func FindOneAndUpdate[T any](ctx context.Context, coll *mongo.Collection, filter any, update any, ...) (res T, err error)
- func FindPage[T any](ctx context.Context, coll *mongo.Collection, filter any, ...) (int64, []T, error)
- func FindPageOnly[T any](ctx context.Context, coll *mongo.Collection, filter any, ...) ([]T, error)
- func Ignore[T any](_ T, err error) error
- func IgnoreWarp[T any](_ T, err error) error
- func IncrVersion(dbs ...func() error) error
- func InsertMany[T any](ctx context.Context, coll *mongo.Collection, val []T, ...) error
- func InsertOne[T any](ctx context.Context, coll *mongo.Collection, val T, ...) error
- func NewMongo(client *mongo.Client) tx.Tx
- func NewMongoTx(ctx context.Context, client *mongo.Client) (tx.Tx, error)
- func UpdateMany(ctx context.Context, coll *mongo.Collection, filter any, update any, ...) (*mongo.UpdateResult, error)
- func UpdateOne(ctx context.Context, coll *mongo.Collection, filter any, update any, ...) error
- func UpdateOneResult(ctx context.Context, coll *mongo.Collection, filter any, update any, ...) (*mongo.UpdateResult, error)
- type Client
- type Config
- type ReadPrefConfig
- type ReplicaSetConfig
- type WriteConcernConfig
Constants ¶
View Source
const ( StandaloneMode = "standalone" // Standalone MongoDB mode ReplicaSetMode = "replicaSet" // Replica set MongoDB mode )
MongoMode
View Source
const ( PrimaryMode = "primary" // Primary read preference mode PrimaryPreferredMode = "primaryPreferred" // Primary preferred read preference mode SecondaryMode = "secondary" // Secondary read preference mode SecondaryPreferredMode = "secondaryPreferred" // Secondary preferred read preference mode NearestMode = "nearest" // Nearest read preference mode )
ReadPreference
View Source
const ( LocalReadConcern = "local" // Local read concern level AvailableReadConcern = "available" // Available read concern level MajorityReadConcern = "majority" // Majority read concern level LinearizableReadConcern = "linearizable" // Linearizable read concern level SnapshotReadConcern = "snapshot" // Snapshot read concern level )
ReadConcern levels
View Source
const (
MajorityWriteConcern = "majority" // Majority write concern level
)
WriteConcern
Variables ¶
This section is empty.
Functions ¶
func Aggregate ¶
func Aggregate[T any](ctx context.Context, coll *mongo.Collection, pipeline any, opts ...*options.AggregateOptions) ([]T, error)
func Count ¶
func Count(ctx context.Context, coll *mongo.Collection, filter any, opts ...*options.CountOptions) (int64, error)
func DeleteMany ¶
func DeleteMany(ctx context.Context, coll *mongo.Collection, filter any, opts ...*options.DeleteOptions) error
func DeleteManyResult ¶ added in v0.0.49
func DeleteManyResult(ctx context.Context, coll *mongo.Collection, filter any, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
func DeleteOne ¶
func DeleteOne(ctx context.Context, coll *mongo.Collection, filter any, opts ...*options.DeleteOptions) error
func DeleteOneResult ¶ added in v0.0.49
func DeleteOneResult(ctx context.Context, coll *mongo.Collection, filter any, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
func Exist ¶
func Exist(ctx context.Context, coll *mongo.Collection, filter any, opts ...*options.CountOptions) (bool, error)
func Find ¶
func Find[T any](ctx context.Context, coll *mongo.Collection, filter any, opts ...*options.FindOptions) ([]T, error)
func FindOne ¶
func FindOne[T any](ctx context.Context, coll *mongo.Collection, filter any, opts ...*options.FindOneOptions) (res T, err error)
func FindOneAndUpdate ¶
func FindOneAndUpdate[T any](ctx context.Context, coll *mongo.Collection, filter any, update any, opts ...*options.FindOneAndUpdateOptions) (res T, err error)
func FindPage ¶
func FindPage[T any](ctx context.Context, coll *mongo.Collection, filter any, pagination pagination.Pagination, opts ...*options.FindOptions) (int64, []T, error)
func FindPageOnly ¶
func FindPageOnly[T any](ctx context.Context, coll *mongo.Collection, filter any, pagination pagination.Pagination, opts ...*options.FindOptions) ([]T, error)
func IgnoreWarp ¶ added in v0.0.49
func IncrVersion ¶ added in v0.0.49
func InsertMany ¶
func InsertMany[T any](ctx context.Context, coll *mongo.Collection, val []T, opts ...*options.InsertManyOptions) error
func InsertOne ¶
func InsertOne[T any](ctx context.Context, coll *mongo.Collection, val T, opts ...*options.InsertOneOptions) error
func UpdateMany ¶
func UpdateMany(ctx context.Context, coll *mongo.Collection, filter any, update any, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
func UpdateOne ¶
func UpdateOne(ctx context.Context, coll *mongo.Collection, filter any, update any, notMatchedErr bool, opts ...*options.UpdateOptions) error
func UpdateOneResult ¶
func UpdateOneResult(ctx context.Context, coll *mongo.Collection, filter any, update any, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func NewMongoDB ¶
NewMongoDB initializes a new MongoDB connection.
type Config ¶
type Config struct {
Uri string
Address []string
Database string
Username string
Password string
AuthSource string
MaxPoolSize int
MaxRetry int
MongoMode string // "replicaSet" or "standalone"
ReplicaSet *ReplicaSetConfig
ReadPreference *ReadPrefConfig
WriteConcern *WriteConcernConfig
}
Config represents the MongoDB configuration.
func (*Config) ValidateAndSetDefaults ¶
ValidateAndSetDefaults validates the configuration and sets default values.
type ReadPrefConfig ¶
type ReadPrefConfig struct {
Mode string `json:"mode" yaml:"mode" config:"allowempty"` // primary, secondary, secondaryPreferred, nearest
TagSets []map[string]string `json:"tagSets" yaml:"tagSets" config:"allowempty"` // Tag sets
MaxStaleness time.Duration `json:"maxStaleness" yaml:"maxStaleness" config:"allowempty"` // Maximum staleness time
}
type ReplicaSetConfig ¶
type ReplicaSetConfig struct {
Name string `json:"name" yaml:"name" config:"allowempty"` // Replica set name
Hosts []string `json:"hosts" yaml:"hosts" config:"allowempty"` // Replica set host list
ReadConcern string `json:"readConcern" yaml:"readConcern" config:"allowempty"` // Read concern level: local, available, majority, linearizable, snapshot
MaxStaleness time.Duration `json:"maxStaleness" yaml:"maxStaleness" config:"allowempty"` // Maximum staleness time
}
type WriteConcernConfig ¶
type WriteConcernConfig struct {
W any `json:"w" yaml:"w" config:"allowempty"` // Write node count or tag (int, "majority", or custom tag)
J bool `json:"j" yaml:"j" config:"allowempty"` // Whether to wait for journal confirmation
WTimeout time.Duration `json:"wtimeout" yaml:"wtimeout" config:"allowempty"` // Write timeout duration
}
Click to show internal directories.
Click to hide internal directories.