Documentation
¶
Overview ¶
Package mongodb provides MongoDB client and document helpers.
New code should use explicit client ownership and core-owned document contracts:
import "github.com/InsideGallery/core/db/mongodb" store := mongodb.NewClientStore(nil) client, err := store.GetOrCreate(config)
Prefer DocumentStore with FindOptions, CountOptions, InsertOptions, UpdateOptions, DeleteOptions, DocumentResult, and WriteResult for application boundaries that should not expose MongoDB SDK result types.
Compatibility: package-level Set, Get, and Default remain available for existing consumers. Prefer NewMongoClient or ClientStore.GetOrCreate with explicit configuration in new code.
Index ¶
- Constants
- Variables
- func Clone(oldObj interface{}) interface{}
- func GetBsonD(keyValue ...interface{}) bson.Ddeprecated
- func NewSort(fields ...SortField) any
- func Set(r *MongoClient)deprecated
- type Clientdeprecated
- type ClientStore
- type ConnectionConfig
- type CountOptions
- type DeleteOptions
- type Document
- type DocumentResult
- type DocumentStore
- type Field
- type Filter
- type FindOptions
- type InsertOptions
- 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) Count(ctx context.Context, options CountOptions) (DocumentResult, error)
- 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) DeleteDocuments(ctx context.Context, options DeleteOptions) (WriteResult, 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) FindDocuments(ctx context.Context, options FindOptions) (DocumentResult, 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) FindOneDocument(ctx context.Context, options FindOptions) (DocumentResult, error)
- func (m *MongoClient) InsertDocument(ctx context.Context, options InsertOptions) (WriteResult, 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) UpdateDocuments(ctx context.Context, options UpdateOptions) (WriteResult, 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
- type SortField
- type UpdateOptions
- type WriteResult
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
var ErrDocumentTargetIsNotSet = errors.New("document target is not set")
ErrDocumentTargetIsNotSet reports a missing decode target for document reads.
var ErrFilterKeyType = errors.New("filter key must be a string")
ErrFilterKeyType reports a non-string filter key.
var ErrFilterPairCount = errors.New("filter pairs must contain key and value")
ErrFilterPairCount reports an odd key/value filter input.
Functions ¶
func NewSort ¶ added in v1.1.0
NewSort creates a MongoDB-compatible sort document without exposing BSON types in the public signature.
func Set
deprecated
Types ¶
type Client
deprecated
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
}
Client is the legacy MongoDB SDK-shaped client contract.
Deprecated: use DocumentStore and core-owned option/result types for new code.
type ClientStore ¶ added in v1.1.0
type ClientStore struct {
// contains filtered or unexported fields
}
ClientStore owns a MongoDB client for explicit application composition.
func NewClientStore ¶ added in v1.1.0
func NewClientStore(client *MongoClient) *ClientStore
NewClientStore creates a MongoDB client store with an optional existing client.
func (*ClientStore) Close ¶ added in v1.1.0
func (s *ClientStore) Close(ctx context.Context) error
Close disconnects the stored MongoDB client and clears this store.
func (*ClientStore) Get ¶ added in v1.1.0
func (s *ClientStore) Get() (*MongoClient, error)
Get returns the MongoDB client from this store.
func (*ClientStore) GetOrCreate ¶ added in v1.1.0
func (s *ClientStore) GetOrCreate(config *ConnectionConfig) (*MongoClient, error)
GetOrCreate returns or creates a MongoDB client from explicit config.
func (*ClientStore) Set ¶ added in v1.1.0
func (s *ClientStore) Set(client *MongoClient)
Set stores a MongoDB client in this store.
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 CountOptions ¶ added in v1.1.0
CountOptions is the core-owned input for MongoDB count operations.
type DeleteOptions ¶ added in v1.1.0
DeleteOptions is the core-owned input for MongoDB delete operations.
type Document ¶ added in v1.1.0
Document is a core-owned MongoDB document shape.
func DocumentFromPairs ¶ added in v1.1.0
DocumentFromPairs creates a core-owned MongoDB document from key/value pairs.
func NewDocument ¶ added in v1.1.0
NewDocument creates a core-owned MongoDB document.
type DocumentResult ¶ added in v1.1.0
DocumentResult is the core-owned result for MongoDB read operations.
type DocumentStore ¶ added in v1.1.0
type DocumentStore interface {
FindOneDocument(ctx context.Context, options FindOptions) (DocumentResult, error)
FindDocuments(ctx context.Context, options FindOptions) (DocumentResult, error)
Count(ctx context.Context, options CountOptions) (DocumentResult, error)
InsertDocument(ctx context.Context, options InsertOptions) (WriteResult, error)
UpdateDocuments(ctx context.Context, options UpdateOptions) (WriteResult, error)
DeleteDocuments(ctx context.Context, options DeleteOptions) (WriteResult, error)
}
DocumentStore is the core-owned MongoDB contract for new consumers.
type Filter ¶ added in v1.1.0
Filter is a core-owned MongoDB filter document.
func FilterFromPairs ¶ added in v1.1.0
FilterFromPairs creates a core-owned MongoDB filter from key/value pairs.
type FindOptions ¶ added in v1.1.0
FindOptions is the core-owned input for MongoDB read operations.
type InsertOptions ¶ added in v1.1.0
InsertOptions is the core-owned input for MongoDB insert operations.
type MongoClient ¶
MongoClient client for mongo db
func Default
deprecated
func Default() (*MongoClient, error)
Default return default client
Deprecated: use NewMongoClient or ClientStore.GetOrCreate with explicit config.
func Get
deprecated
func Get() (*MongoClient, error)
Get return mongo client
Deprecated: use ClientStore.Get on an explicit store.
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) Count ¶ added in v1.1.0
func (m *MongoClient) Count(ctx context.Context, options CountOptions) (DocumentResult, error)
Count counts matching documents with core-owned options.
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) DeleteDocuments ¶ added in v1.1.0
func (m *MongoClient) DeleteDocuments(ctx context.Context, options DeleteOptions) (WriteResult, error)
DeleteDocuments deletes one or many documents with core-owned options.
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) FindDocuments ¶ added in v1.1.0
func (m *MongoClient) FindDocuments(ctx context.Context, options FindOptions) (DocumentResult, error)
FindDocuments reads documents with core-owned options.
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) FindOneDocument ¶ added in v1.1.0
func (m *MongoClient) FindOneDocument(ctx context.Context, options FindOptions) (DocumentResult, error)
FindOneDocument reads one document with core-owned options.
func (*MongoClient) InsertDocument ¶ added in v1.1.0
func (m *MongoClient) InsertDocument(ctx context.Context, options InsertOptions) (WriteResult, error)
InsertDocument inserts one document with core-owned options.
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) UpdateDocuments ¶ added in v1.1.0
func (m *MongoClient) UpdateDocuments(ctx context.Context, options UpdateOptions) (WriteResult, error)
UpdateDocuments updates one or many documents with core-owned options.
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
type UpdateOptions ¶ added in v1.1.0
UpdateOptions is the core-owned input for MongoDB update operations.