Documentation ¶
Overview ¶
Package mongo implement common logic interaction with mongodb
Index ¶
- type BaseMongoRepository
- type CollectionStats
- type DBResult
- type Db
- func (db *Db) Aggregate(ctx context.Context, coll *mongo.Collection, pipe interface{}, ...) error
- func (db *Db) CollectionStats(ctx context.Context, coll *mongo.Collection) (*CollectionStats, error)
- func (db *Db) Count(ctx context.Context, coll *mongo.Collection, filter interface{}) (int64, error)
- func (db *Db) Database() *mongo.Database
- func (db *Db) Delete(ctx context.Context, coll *mongo.Collection, filter interface{}) error
- func (db *Db) DeleteByID(ctx context.Context, coll *mongo.Collection, id string) error
- func (db *Db) Disconnect(ctx context.Context) error
- func (db *Db) Find(ctx context.Context, coll *mongo.Collection, filter interface{}, ...) error
- func (db *Db) GetCollection(name string) *mongo.Collection
- func (db *Db) GetOne(ctx context.Context, coll *mongo.Collection, filter interface{}, ...) error
- func (db *Db) GetOneByID(ctx context.Context, coll *mongo.Collection, id string, document interface{}) error
- func (db *Db) InsertOne(ctx context.Context, coll *mongo.Collection, document interface{}) (string, error)
- func (db *Db) Ping(ctx context.Context) error
- func (db *Db) ReplaceOne(ctx context.Context, coll *mongo.Collection, filter interface{}, ...) error
- func (db *Db) UpdateOne(ctx context.Context, coll *mongo.Collection, filter interface{}, ...) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BaseMongoRepository ¶
type BaseMongoRepository interface { db.BaseRepository // Database return current mongo database Database() *mongo.Database // GetCollection returns reference to mongo.Collection (table) GetCollection(name string) *mongo.Collection // InsertOne executes an insert command to insert a single document into the collection. InsertOne(ctx context.Context, coll *mongo.Collection, document interface{}) (string, error) // GetOne returns document looked up by filter, or error GetOne(ctx context.Context, coll *mongo.Collection, filter interface{}, document interface{}) error // GetOneByID returns document looked up by id, or error GetOneByID(ctx context.Context, coll *mongo.Collection, id string, document interface{}) error // Delete deletes a stored document(s) by provided filter Delete(ctx context.Context, coll *mongo.Collection, filter interface{}) error // DeleteByID deletes a stored document with provided id DeleteByID(ctx context.Context, coll *mongo.Collection, id string) error // Find returns all documents matching to the filter Find(ctx context.Context, coll *mongo.Collection, filter interface{}, docs interface{}) error // ReplaceOne replace a single document looked up by filter ReplaceOne(ctx context.Context, coll *mongo.Collection, filter interface{}, document interface{}) error // UpdateOne updates a fields in single document looked up by filter UpdateOne(ctx context.Context, coll *mongo.Collection, filter interface{}, update interface{}) error // Count returns count of documents looked up by filter Count(ctx context.Context, coll *mongo.Collection, filter interface{}) (int64, error) // CollectionStats returns general statistics about mongodb collection CollectionStats(ctx context.Context, coll *mongo.Collection) (*CollectionStats, error) }
BaseMongoRepository base MongoDb repository functionality
type CollectionStats ¶ added in v1.1.3
type CollectionStats struct { // Size is collection size in bytes Size int64 `json:"size"` //AvgObjSize is Average object size in bytes AvgObjSize int `json:"avgObjSize"` //StorageSize is storage size in bytes StorageSize int64 `json:"storageSize"` //TotalIndexSize isTotal index size in bytes TotalIndexSize int64 `json:"totalIndexSize"` }
CollectionStats collection statistics
type Db ¶
type Db struct { Timeout time.Duration BaseMongoRepository // contains filtered or unexported fields }
Db service managing connection to MongoDb instance
func NewMongoDB ¶
NewMongoDB create a new Db instance, with the connection URI provided
func (*Db) Aggregate ¶
func (db *Db) Aggregate(ctx context.Context, coll *mongo.Collection, pipe interface{}, opts *options.AggregateOptions, documents interface{}) error
Aggregate execute custom aggregate query
func (*Db) CollectionStats ¶ added in v1.1.3
func (db *Db) CollectionStats(ctx context.Context, coll *mongo.Collection) (*CollectionStats, error)
CollectionStats returns general statistics about mongodb collection
func (*Db) DeleteByID ¶
DeleteByID deletes a stored document
func (*Db) Disconnect ¶
Disconnect close sockets to DB
func (*Db) Find ¶
func (db *Db) Find(ctx context.Context, coll *mongo.Collection, filter interface{}, docs interface{}) error
Find returns all documents matching to the filter
func (*Db) GetCollection ¶
func (db *Db) GetCollection(name string) *mongo.Collection
GetCollection returns reference to mongo.Collection (table)
func (*Db) GetOne ¶
func (db *Db) GetOne(ctx context.Context, coll *mongo.Collection, filter interface{}, document interface{}) error
GetOne returns document looked up by filter, or error
func (*Db) GetOneByID ¶
func (db *Db) GetOneByID(ctx context.Context, coll *mongo.Collection, id string, document interface{}) error
GetOneByID returns document looked up by id, or error
func (*Db) InsertOne ¶
func (db *Db) InsertOne(ctx context.Context, coll *mongo.Collection, document interface{}) (string, error)
InsertOne executes an insert command to insert a single document into the collection.
func (*Db) ReplaceOne ¶
func (db *Db) ReplaceOne(ctx context.Context, coll *mongo.Collection, filter interface{}, document interface{}) error
ReplaceOne replace a single document looked up by filter