Documentation
¶
Overview ¶
Package mongodb implements the benpate/data interfaces (Server, Session, Collection, Iterator) on top of the official MongoDB Go driver, so that application code can perform CRUD operations against MongoDB without depending on the driver directly.
Index ¶
- func ExpressionToBSON(criteria exp.Expression) bson.M
- func SetLogTimeout(timeout int)
- type Collection
- func (c Collection) Context() context.Context
- func (c Collection) Count(criteria exp.Expression, options ...option.Option) (int64, error)
- func (c Collection) Delete(object data.Object, note string) error
- func (c Collection) HardDelete(criteria exp.Expression) error
- func (c Collection) Iterator(criteria exp.Expression, options ...option.Option) (data.Iterator, error)
- func (c Collection) Load(criteria exp.Expression, target data.Object, options ...option.Option) error
- func (c Collection) Mongo() *mongo.Collection
- func (c Collection) Query(target any, criteria exp.Expression, options ...option.Option) error
- func (c Collection) Save(object data.Object, note string) error
- type Iterator
- type Server
- type Session
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExpressionToBSON ¶
func ExpressionToBSON(criteria exp.Expression) bson.M
ExpressionToBSON converts a data.Expression value into pure bson.
func SetLogTimeout ¶ added in v0.30.2
func SetLogTimeout(timeout int)
SetLogTimeout configures the threshold (in milliseconds) above which slow queries are logged. A value of zero or less disables slow-query logging.
Types ¶
type Collection ¶
type Collection struct {
// contains filtered or unexported fields
}
Collection wraps a mongodb.Collection with all of the methods required by the data.Collection interface
func NewCollection ¶ added in v0.28.3
func NewCollection(collection *mongo.Collection) Collection
NewCollection creates a new Collection object directly from a mongo.Collection
func (Collection) Context ¶ added in v0.30.4
func (c Collection) Context() context.Context
Context returns the context associated with this collection.
func (Collection) Count ¶ added in v0.28.0
func (c Collection) Count(criteria exp.Expression, options ...option.Option) (int64, error)
Count returns the number of records in the collection that match the provided criteria.
func (Collection) Delete ¶
func (c Collection) Delete(object data.Object, note string) error
Delete removes a single object from the database, using a "virtual delete"
func (Collection) HardDelete ¶ added in v0.1.0
func (c Collection) HardDelete(criteria exp.Expression) error
HardDelete physically removes an object from the database.
func (Collection) Iterator ¶ added in v0.28.0
func (c Collection) Iterator(criteria exp.Expression, options ...option.Option) (data.Iterator, error)
Iterator retrieves a group of objects from the database as an iterator
func (Collection) Load ¶
func (c Collection) Load(criteria exp.Expression, target data.Object, options ...option.Option) error
Load retrieves a single object from the database
func (Collection) Mongo ¶
func (c Collection) Mongo() *mongo.Collection
Mongo returns the underlying mongodb collection for libraries that need to bypass this abstraction.
func (Collection) Query ¶ added in v0.2.2
func (c Collection) Query(target any, criteria exp.Expression, options ...option.Option) error
Query retrieves a group of objects from the database and populates a target interface
type Iterator ¶
type Iterator struct {
// contains filtered or unexported fields
}
Iterator wraps the mongodb Cursor object
func NewIterator ¶
NewIterator returns a fully populated Iterator object
func (Iterator) Close ¶
Close closes the wrapped Cursor. A cursor-less iterator has nothing to close.
func (Iterator) Count ¶
Count returns the total number of records contained by this iterator. A cursor-less iterator (for example, one returned alongside an error) is empty.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is an abstract representation of a MongoDB database. It implements the data.Server interface, so that it should be usable anywhere that requires a data.Server.
func New ¶
New returns a fully populated mongodb.Server. It requires that you provide the URI for the mongodb cluster, along with the name of the database to be used for all transactions.
func (Server) Client ¶ added in v0.30.4
Client returns the underlying mongodb client for libraries that need to bypass this abstraction.
func (Server) Database ¶ added in v0.30.4
Database returns the underlying mongodb database for libraries that need to bypass this abstraction.
func (Server) Session ¶
Session returns a new client session that can be used to perform CRUD transactions on this datastore.
func (Server) WithTransaction ¶ added in v0.30.4
func (server Server) WithTransaction(ctx context.Context, fn data.TransactionCallbackFunc) (any, error)
WithTransaction runs fn inside a MongoDB transaction, committing if it returns nil and rolling back if it returns an error. Transactions require the server to be a replica set or mongos.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session represents a single database session, such as a session encompassing all of the database queries to respond to a single REST service call.
func NewSession ¶ added in v0.28.3
NewSession generates a new Session object from a mongo.Database
func (Session) Close ¶
func (s Session) Close()
Close is a no-op that exists to satisfy the data.Session interface. This Session holds no per-session resources: connections are owned by the long-lived *mongo.Client pool and recycled automatically after each operation. Callers release per-request resources by cancelling the context they passed to Server.Session, not by calling Close.
func (Session) Collection ¶
func (s Session) Collection(collection string) data.Collection
Collection returns a reference to an individual database collection.