Documentation ¶
Index ¶
- func IsValidObjectHex(id string) bool
- func IsValidObjectID(id bson.ObjectId) bool
- type Connection
- func (conn *Connection) Collection() *mgo.Collection
- func (conn *Connection) Connect() *Connection
- func (conn *Connection) Delete(query interface{}) error
- func (conn *Connection) Disconnect()
- func (conn *Connection) GetByID(id interface{}, i interface{}) error
- func (conn *Connection) GetLimitFields(q, selector, doc interface{}, fields ...string) error
- func (conn *Connection) GetLimitN(q, selector, doc interface{}, n int, fields ...string) error
- func (conn *Connection) GetLimitedRecords(q interface{}, n int, doc interface{}, fields ...string) error
- func (conn *Connection) GetMany(q interface{}, doc interface{}, fields ...string) error
- func (conn *Connection) GetUniqueOne(q interface{}, doc interface{}) error
- func (conn *Connection) Insert(doc interface{}) error
- func (conn *Connection) IterAll(pipeline interface{}, i interface{}) error
- func (conn *Connection) Update(query interface{}, i interface{}) error
- func (conn *Connection) UpdateByQueryField(q interface{}, field string, value interface{}) (*mgo.ChangeInfo, error)
- func (conn *Connection) Upsert(query interface{}, i interface{}) (*mgo.ChangeInfo, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsValidObjectHex ¶
IsValidObjectHex check whether the id can be converted to be a qualified ObjectId
func IsValidObjectID ¶
IsValidObjectID check whether the id is a qualified ObjectId
Types ¶
type Connection ¶
Connection represents a MongoDB collection.
func NewConnection ¶
func NewConnection(s *mgo.Session, db, cname string) *Connection
NewConnection creates a connection to MongoDB.
func (*Connection) Collection ¶
func (conn *Connection) Collection() *mgo.Collection
func (*Connection) Delete ¶
func (conn *Connection) Delete(query interface{}) error
Delete remove documents from a collection.
func (*Connection) GetByID ¶
func (conn *Connection) GetByID(id interface{}, i interface{}) error
GetByID get a single record by ID
func (*Connection) GetLimitFields ¶
func (conn *Connection) GetLimitFields(q, selector, doc interface{}, fields ...string) error
GetLimitFields gets records by query, and only retrieve the name field by selector. If fields are provided, asks the database to order returned documents according to the provided field names.
func (*Connection) GetLimitN ¶
func (conn *Connection) GetLimitN(q, selector, doc interface{}, n int, fields ...string) error
GetLimitN gets records by query, and only retrieve the name field by selector. If fields are provided, asks the database to order returned documents according to the provided field names, then restricts the maximum number of documents retrieved to n. For example:
q := bson.M{"_id": bson.M{"$lt": id},"status": true} selector := bson.M{"title": 1, "_id": 1, "abstract": 1, "created": 1} n = 10 fields := []string{"-$natural"} doc := []Article err := GetLimitN(q, selector, doc, n, fields)
func (*Connection) GetLimitedRecords ¶
func (conn *Connection) GetLimitedRecords(q interface{}, n int, doc interface{}, fields ...string) error
GetLimitedRecords obtain records based on specified conditions. The results of the specified number of returns are sorted by the specified fields.
func (*Connection) GetMany ¶
func (conn *Connection) GetMany(q interface{}, doc interface{}, fields ...string) error
GetMany get multiple records based on a condition
func (*Connection) GetUniqueOne ¶
func (conn *Connection) GetUniqueOne(q interface{}, doc interface{}) error
GetUniqueOne get a single record by query
func (*Connection) Insert ¶
func (conn *Connection) Insert(doc interface{}) error
Insert add new documents to a collection.
func (*Connection) IterAll ¶
func (conn *Connection) IterAll(pipeline interface{}, i interface{}) error
IterAll prepares a pipeline to aggregate and executes the pipeline, works like Iter.All.
func (*Connection) Update ¶
func (conn *Connection) Update(query interface{}, i interface{}) error
Update modify existing documents in a collection.
func (*Connection) UpdateByQueryField ¶
func (conn *Connection) UpdateByQueryField(q interface{}, field string, value interface{}) (*mgo.ChangeInfo, error)
UpdateByQueryField modify all eligible documents.
func (*Connection) Upsert ¶
func (conn *Connection) Upsert(query interface{}, i interface{}) (*mgo.ChangeInfo, error)
Upsert creates a new document and inserts it if no documents match the specified filter. If there are matching documents, then the operation modifies or replaces the matching document or documents.