Documentation
¶
Index ¶
- Constants
- Variables
- func Clone(oldObj interface{}) interface{}
- func GetBsonD(keyValue ...interface{}) bson.D
- func Set(r *MongoClient)
- type Client
- type ConnectionConfig
- type MongoClient
- func (m *MongoClient) Aggregate(ctx context.Context, collection string, value interface{}, ...) ([]interface{}, error)
- func (m *MongoClient) BatchUpdateByID(ctx context.Context, collection string, data map[interface{}]interface{}) error
- func (m *MongoClient) Collection(name string, opts ...*options.CollectionOptions) *mongo.Collection
- func (m *MongoClient) Connection() *mongo.Client
- func (m *MongoClient) CountDocuments(ctx context.Context, collection string, filter interface{}, ...) (int64, error)
- func (m *MongoClient) Database(name string, opts ...*options.DatabaseOptions) *mongo.Database
- func (m *MongoClient) DeleteCollection(ctx context.Context, name string) error
- func (m *MongoClient) DeleteMany(ctx context.Context, collection string, filter interface{}, ...) error
- func (m *MongoClient) DeleteOne(ctx context.Context, collection string, filter interface{}, ...) error
- func (m *MongoClient) Drop(ctx context.Context, collection string) error
- func (m *MongoClient) Find(ctx context.Context, collection string, value interface{}, filter interface{}, ...) ([]interface{}, error)
- func (m *MongoClient) FindOne(ctx context.Context, collection string, value interface{}, filter interface{}, ...) error
- func (m *MongoClient) FindOneByID(ctx context.Context, collection string, value interface{}, id interface{}, ...) error
- func (m *MongoClient) InsertMany(ctx context.Context, collection string, documents []interface{}, ...) error
- func (m *MongoClient) InsertOne(ctx context.Context, collection string, value interface{}, ...) error
- func (m *MongoClient) UpdateByObject(ctx context.Context, collection string, value interface{}, filter interface{}, ...) error
- func (m *MongoClient) UpsertMany(ctx context.Context, collection string, keys []interface{}, ...) error
- func (m *MongoClient) UpsertManyByFilter(ctx context.Context, collection string, filterBy string, keys []interface{}, ...) error
- func (m *MongoClient) UpsertOne(ctx context.Context, collection string, update interface{}, filter interface{}, ...) error
- func (m *MongoClient) WithDB(name string) Client
Constants ¶
const EnvPrefix = "MONGO"
EnvPrefix environment prefix for mongodb config
const (
TwoPrecision = 2
)
Variables ¶
var (
ErrConnectionIsNotSet = errors.New("connection is not set")
)
All kind of errors for mongo
Functions ¶
Types ¶
type Client ¶
type Client interface {
FindOne(
ctx context.Context,
collection string, value interface{}, filter interface{}, opts ...*options.FindOneOptions) error
Find(
ctx context.Context,
collection string,
value interface{},
filter interface{},
opts ...*options.FindOptions,
) ([]interface{}, error)
FindOneByID(
ctx context.Context,
collection string,
value interface{},
id interface{},
opts ...*options.FindOneOptions,
) error
CountDocuments(
ctx context.Context,
collection string,
filter interface{},
opts ...*options.CountOptions,
) (int64, error)
Aggregate(
ctx context.Context,
collection string,
value interface{},
pipeline interface{},
opts ...*options.AggregateOptions,
) ([]interface{}, error)
InsertOne(
ctx context.Context,
collection string,
value interface{},
opts ...*options.InsertOneOptions,
) error
UpsertOne(
ctx context.Context,
collection string,
update interface{},
filter interface{},
opts ...*options.UpdateOptions,
) error
UpsertMany(
ctx context.Context,
collection string,
keys []interface{},
documents []interface{},
) error
UpsertManyByFilter(
ctx context.Context,
collection string,
filterBy string,
keys []interface{},
documents []interface{},
) error
UpdateByObject(
ctx context.Context,
collection string,
value interface{},
filter interface{},
opts ...*options.UpdateOptions,
) error
DeleteOne(ctx context.Context, collection string, filter interface{}, opts ...*options.DeleteOptions) error
DeleteMany(ctx context.Context, collection string, filter interface{}, opts ...*options.DeleteOptions) error
Drop(ctx context.Context, collection string) error
InsertMany(ctx context.Context, collection string, documents []interface{}, opts ...*options.InsertManyOptions) error
Collection(name string, opts ...*options.CollectionOptions) *mongo.Collection
Database(name string, opts ...*options.DatabaseOptions) *mongo.Database
WithDB(name string) Client
Connection() *mongo.Client
BatchUpdateByID(ctx context.Context, collection string, data map[interface{}]interface{}) error
DeleteCollection(ctx context.Context, name string) error
}
type ConnectionConfig ¶
type ConnectionConfig struct {
Hosts []string `env:"_HOSTS" envDefault:"localhost:27017"`
User string `env:"_USER" envDefault:""`
Pass string `env:"_PASS" envDefault:""`
Scheme string `env:"_SCHEME" envDefault:"mongodb"`
Database string `env:"_DATABASE" envDefault:"default"`
Args string `env:"_ARGS" envDefault:""`
Mode string `env:"_MODE" envDefault:"secondarypreferred"`
RetryWrites bool `env:"_RETRYWRITES" envDefault:"false"`
AuthMechanism string `env:"_AUTH_MECHANISM" envDefault:"SCRAM-SHA-256"`
AuthSource string `env:"_AUTH_SOURCE" envDefault:""`
}
ConnectionConfig contains required data for mongo
func GetConnectionConfigFromEnv ¶
func GetConnectionConfigFromEnv() (*ConnectionConfig, error)
GetConnectionConfigFromEnv return mongodb configs bases on environment variables
func (*ConnectionConfig) GetDSN ¶
func (c *ConnectionConfig) GetDSN() string
type MongoClient ¶
MongoClient client for mongo db
func NewMongoClient ¶
func NewMongoClient(config *ConnectionConfig) (*MongoClient, error)
NewMongoClient return client from config
func (*MongoClient) Aggregate ¶
func (m *MongoClient) Aggregate( ctx context.Context, collection string, value interface{}, pipeline interface{}, opts ...*options.AggregateOptions, ) ([]interface{}, error)
Aggregate aggregate rows
func (*MongoClient) BatchUpdateByID ¶
func (m *MongoClient) BatchUpdateByID(ctx context.Context, collection string, data map[interface{}]interface{}) error
func (*MongoClient) Collection ¶
func (m *MongoClient) Collection(name string, opts ...*options.CollectionOptions) *mongo.Collection
func (*MongoClient) Connection ¶
func (m *MongoClient) Connection() *mongo.Client
func (*MongoClient) CountDocuments ¶
func (m *MongoClient) CountDocuments( ctx context.Context, collection string, filter interface{}, opts ...*options.CountOptions, ) (int64, error)
CountDocuments count documents
func (*MongoClient) Database ¶
func (m *MongoClient) Database(name string, opts ...*options.DatabaseOptions) *mongo.Database
func (*MongoClient) DeleteCollection ¶
func (m *MongoClient) DeleteCollection(ctx context.Context, name string) error
func (*MongoClient) DeleteMany ¶
func (m *MongoClient) DeleteMany( ctx context.Context, collection string, filter interface{}, opts ...*options.DeleteOptions, ) error
DeleteMany delete all elements by condition
func (*MongoClient) DeleteOne ¶
func (m *MongoClient) DeleteOne( ctx context.Context, collection string, filter interface{}, opts ...*options.DeleteOptions, ) error
DeleteOne delete one element
func (*MongoClient) Drop ¶
func (m *MongoClient) Drop(ctx context.Context, collection string) error
Drop collection
func (*MongoClient) Find ¶
func (m *MongoClient) Find( ctx context.Context, collection string, value interface{}, filter interface{}, opts ...*options.FindOptions, ) ([]interface{}, error)
Find all and return
func (*MongoClient) FindOne ¶
func (m *MongoClient) FindOne( ctx context.Context, collection string, value interface{}, filter interface{}, opts ...*options.FindOneOptions, ) error
FindOne find and decode one element
func (*MongoClient) FindOneByID ¶
func (m *MongoClient) FindOneByID( ctx context.Context, collection string, value interface{}, id interface{}, opts ...*options.FindOneOptions, ) error
FindOneByID return value by _id
func (*MongoClient) InsertMany ¶
func (m *MongoClient) InsertMany( ctx context.Context, collection string, documents []interface{}, opts ...*options.InsertManyOptions, ) error
InsertMany insert many documents
func (*MongoClient) InsertOne ¶
func (m *MongoClient) InsertOne( ctx context.Context, collection string, value interface{}, opts ...*options.InsertOneOptions, ) error
InsertOne insert single object
func (*MongoClient) UpdateByObject ¶
func (m *MongoClient) UpdateByObject( ctx context.Context, collection string, value interface{}, filter interface{}, opts ...*options.UpdateOptions, ) error
UpdateByObject update to an object by filter
func (*MongoClient) UpsertMany ¶
func (m *MongoClient) UpsertMany( ctx context.Context, collection string, keys []interface{}, documents []interface{}, ) error
UpsertMany insert or update objects
func (*MongoClient) UpsertManyByFilter ¶
func (m *MongoClient) UpsertManyByFilter( ctx context.Context, collection string, filterBy string, keys []interface{}, documents []interface{}, ) error
UpsertManyByFilter insert or update objects by filter
func (*MongoClient) UpsertOne ¶
func (m *MongoClient) UpsertOne( ctx context.Context, collection string, update interface{}, filter interface{}, opts ...*options.UpdateOptions, ) error
UpsertOne insert or update single object
func (*MongoClient) WithDB ¶
func (m *MongoClient) WithDB(name string) Client