Documentation ¶
Index ¶
- Variables
- type DB
- func (db *DB) Aggregate(ctx context.Context, collName string, pipeline interface{}, ...) error
- func (db *DB) CacheKey(collName string, filter interface{}, args ...string) (string, error)
- func (db *DB) Collection(name string) *mongo.Collection
- func (db *DB) Connect(ctx context.Context) error
- func (db *DB) DeleteOne(ctx context.Context, collName, id string) error
- func (db *DB) Disconnect(ctx context.Context) error
- func (db *DB) FindAll(ctx context.Context, collName string, start, limit int64, results interface{}) error
- func (db *DB) FindByID(ctx context.Context, collName, id string, result interface{}) error
- func (db *DB) FindMany(ctx context.Context, collName string, start, limit int64, filter interface{}, ...) error
- func (db *DB) FindOne(ctx context.Context, collName string, filter interface{}, result interface{}) error
- func (db *DB) IDFilter(id string) bson.M
- func (db *DB) InsertOne(ctx context.Context, collName, id string, data interface{}) error
- func (db *DB) Ping(ctx context.Context) error
- func (db *DB) ReplaceByID(ctx context.Context, collName, id string, data interface{}) error
- func (db *DB) SetCacheKeyPrefix(prefix string) *DB
- func (db *DB) SetCacheTTL(d time.Duration) *DB
- func (db *DB) SetCacher(c cache.Cacher) *DB
Constants ¶
This section is empty.
Variables ¶
var ( ErrBadClient = errors.New("mongodb: bad client") ErrBadFilter = errors.New("mongodb: invalid filter") )
var ErrBadDelete = errors.New("mongodb: document does not exist")
var ErrBadFind = errors.New("mongodb: bad collection query")
ErrBadFind serves as a base error to wrap for errors related to a query.
var ErrInsertConflict = errors.New("mongodb: insert failed, document exists")
ErrInsertConflict indicates an existing document of the same ID.
Functions ¶
This section is empty.
Types ¶
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB represents a Mongodb client, with an optional caching layer.
func New ¶
New creates a new DB instance. A default Mongodb client is used. Make sure to call Connect before using the DB.
func NewWithClient ¶
NewWithClient creates a new DB instance, configured with the supplied Mongodb client. Make sure to call Connect before using the DB.
func (*DB) Aggregate ¶
func (db *DB) Aggregate(ctx context.Context, collName string, pipeline interface{}, results interface{}) error
Aggregate uses an aggregation pipeline to process data records and decode into a results data structure. Results must be a pointer to a slice.
func (*DB) Collection ¶
func (db *DB) Collection(name string) *mongo.Collection
Collection returns the named Mongodb collection.
func (*DB) Disconnect ¶
Disconnect connects the Mongodb client with the server. Call this only when done using the DB.
func (*DB) FindAll ¶
func (db *DB) FindAll(ctx context.Context, collName string, start, limit int64, results interface{}) error
FindAll queries for multiple documents, decoding found documents into results. A start index and limit are required. Start must be at least zero, and less than the number of documents in the collection; limit can be zero (to get all documents), or some integer less than or equal to the document count. Note, results must be a pointer to a slice.
func (*DB) FindByID ¶
FindByID queries by the given ID for a single document, decoding the found BSON document into result. Note, result must be a pointer.
func (*DB) FindMany ¶
func (db *DB) FindMany(ctx context.Context, collName string, start, limit int64, filter interface{}, results interface{}) error
FindMany queries for multiple documents, using the given filter, and decodes found documents into results. A start index and limit are required. Start must be at least zero, and less than the number of matched documents in the collection; limit can be zero (to get all matched documents), or some integer less than or equal to the matched document count. Note, results must be a pointer to a slice.
func (*DB) FindOne ¶
func (db *DB) FindOne(ctx context.Context, collName string, filter interface{}, result interface{}) error
FindOne queries by the given filter for a single document, decoding the found BSON document into result. Note, result must be a pointer. By default, a cache is used; this can be bypassed by adding to the context a "no cache" flag, using cache.ContextWithNoCache.
func (*DB) InsertOne ¶
InsertOne tries to insert the given data into the collection. If data is already stored under the same ID, an error is returned.
func (*DB) ReplaceByID ¶
ReplaceByID updates an collection's document by replacing it.
func (*DB) SetCacheKeyPrefix ¶
SetCacher sets the DB's cache key prefix. Note: this is not thread safe. Use this when setting-up a DB.
func (*DB) SetCacheTTL ¶
SetCacher sets the DB's cache time-to-live (TTL). Note: this is not thread safe. Use this when setting-up a DB.