mongodb

package
v0.24.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 22, 2021 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBadClient = errors.New("mongodb: bad client")
	ErrBadFilter = errors.New("mongodb: invalid filter")
)
View Source
var ErrBadDelete = errors.New("mongodb: document does not exist")
View Source
var ErrBadFind = errors.New("mongodb: bad collection query")

ErrBadFind serves as a base error to wrap for errors related to a query.

View Source
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

func New(name string) (*DB, error)

New creates a new DB instance. A default Mongodb client is used. Make sure to call Connect before using the DB.

func NewWithClient

func NewWithClient(name string, client *mongo.Client) *DB

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) CacheKey

func (db *DB) CacheKey(collName string, filter interface{}, args ...string) (string, error)

CacheKey generates a standardized cache key.

func (*DB) Collection

func (db *DB) Collection(name string) *mongo.Collection

Collection returns the named Mongodb collection.

func (*DB) Connect

func (db *DB) Connect(ctx context.Context) error

Connect connects the Mongodb client with the server.

func (*DB) DeleteOne

func (db *DB) DeleteOne(ctx context.Context, collName, id string) error

DeleteOne attempts to delete a document by ID.

func (*DB) Disconnect

func (db *DB) Disconnect(ctx context.Context) error

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

func (db *DB) FindByID(ctx context.Context, collName, id string, result interface{}) error

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) IDFilter

func (db *DB) IDFilter(id string) bson.M

IDFilter returns a filter for querying by ID.

func (*DB) InsertOne

func (db *DB) InsertOne(ctx context.Context, collName, id string, data interface{}) error

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) Ping added in v0.24.2

func (db *DB) Ping(ctx context.Context) error

Ping verifies if the client can connect to the database.

func (*DB) ReplaceByID

func (db *DB) ReplaceByID(ctx context.Context, collName, id string, data interface{}) error

ReplaceByID updates an collection's document by replacing it.

func (*DB) SetCacheKeyPrefix

func (db *DB) SetCacheKeyPrefix(prefix string) *DB

SetCacher sets the DB's cache key prefix. Note: this is not thread safe. Use this when setting-up a DB.

func (*DB) SetCacheTTL

func (db *DB) SetCacheTTL(d time.Duration) *DB

SetCacher sets the DB's cache time-to-live (TTL). Note: this is not thread safe. Use this when setting-up a DB.

func (*DB) SetCacher

func (db *DB) SetCacher(c cache.Cacher) *DB

SetCacher sets the DB's Cacher. Note: this is not thread safe, and does not coordinate handing-off from one Cacher to another. Use this when setting-up a DB.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL