Documentation
¶
Overview ¶
Package mongodb provides a MongoDB implementation of the storage interface.
Index ¶
- Constants
- func Get() storage.IStorage
- func Set(s storage.IStorage)
- func ToMongoString(s customsort.Sort) (bson.D, error)
- type Config
- type MongoDB
- func (m *MongoDB) BulkCreate(ctx context.Context, target string, items []storage.BulkItem, ...) (storage.BulkResult, error)
- func (m *MongoDB) Close() error
- func (m *MongoDB) Count(ctx context.Context, target string, prm *count.Count, ...) (int64, error)
- func (m *MongoDB) Create(ctx context.Context, id, target string, v any, prm *create.Create, ...) (string, error)
- func (m *MongoDB) Delete(ctx context.Context, id, target string, prm *delete.Delete, ...) error
- func (m *MongoDB) GetClient() any
- func (m *MongoDB) List(ctx context.Context, target string, v any, prm *list.List, ...) error
- func (m *MongoDB) Retrieve(ctx context.Context, id, target string, v any, prm *retrieve.Retrieve, ...) error
- func (m *MongoDB) Update(ctx context.Context, id, target string, v any, prm *update.Update, ...) error
Constants ¶
const (
// Name of the storage.
Name = "mongodb"
)
Variables ¶
This section is empty.
Functions ¶
func ToMongoString ¶
func ToMongoString(s customsort.Sort) (bson.D, error)
ToMongoString converts the `Sort` to MongoDB sort format.
Types ¶
type MongoDB ¶
type MongoDB struct {
*storage.Storage
// Client is the MongoDB client.
Client *mongo.Client `json:"-" validate:"required,structonly"`
// Config is the MongoDB configuration.
// Skipped by the validator: it dives into untagged nested structs, and
// this config holds live client state that background goroutines write.
Config *Config `json:"-" validate:"-"`
// Database to connect to.
Database string `json:"database" validate:"required"`
// Target allows to set a static target. If it is empty, the target will be
// dynamic - the one set at the operation (count, create, delete, etc) time.
// Depending on the storage, target is a collection, a table, a bucket, etc.
// For ElasticSearch, for example it doesn't have a concept of a database -
// the target then is the index. Due to different cases of ElasticSearch
// usage, the target can be static or dynamic - defined at the index time,
// for example: log-{YYYY}-{MM}. For Redis, it isn't used at all.
Target string `json:"-" validate:"omitempty,gt=0"`
// contains filtered or unexported fields
}
MongoDB storage definition.
func (*MongoDB) BulkCreate ¶
func (m *MongoDB) BulkCreate( ctx context.Context, target string, items []storage.BulkItem, prm *create.Create, optionFuncs ...storage.Func[*create.Create], ) (storage.BulkResult, error)
BulkCreate inserts documents with unordered InsertMany calls so one write error does not prevent the other documents in the chunk from being tried.
func (*MongoDB) Count ¶
func (m *MongoDB) Count(ctx context.Context, target string, prm *count.Count, options ...storage.Func[*count.Count]) (int64, error)
Count returns the number of items in the storage.
func (*MongoDB) Create ¶
func (m *MongoDB) Create(ctx context.Context, id, target string, v any, prm *create.Create, options ...storage.Func[*create.Create]) (string, error)
Create data.
NOTE: Not all storages returns the ID, neither all storages requires `id` to be set.
WARN: MongoDB relies on the model (`v`) `_id` field to be set, otherwise it will generate a new one. IT'S UP TO THE DEVELOPER TO SET THE `_ID` FIELD.
func (*MongoDB) Delete ¶
func (m *MongoDB) Delete(ctx context.Context, id, target string, prm *delete.Delete, options ...storage.Func[*delete.Delete]) error
Delete removes data.
func (*MongoDB) List ¶
func (m *MongoDB) List(ctx context.Context, target string, v any, prm *list.List, opts ...storage.Func[*list.List]) error
List data.
WARN: In general, projections that include non-indexed fields or fields that are part of a covered index (i.e., an index that includes all the projected fields) are less likely to impact performance.
Any accepts a bson.M filter. Sort, Limit, Offset, and Fields are translated into MongoDB find options; Search is not used.