mongo

package
v0.0.0-...-eb64f0e Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2018 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsValidObjectHex

func IsValidObjectHex(id string) bool

IsValidObjectHex check whether the id can be converted to be a qualified ObjectId

func IsValidObjectID

func IsValidObjectID(id bson.ObjectId) bool

IsValidObjectID check whether the id is a qualified ObjectId

Types

type Connection

type Connection struct {
	Database string
	Name     string
	// contains filtered or unexported fields
}

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

func (conn *Connection) Connect() *Connection

Connect to MongoDB.

func (*Connection) Delete

func (conn *Connection) Delete(query interface{}) error

Delete remove documents from a collection.

func (*Connection) Disconnect

func (conn *Connection) Disconnect()

Disconnect from MongoDB.

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.

Jump to

Keyboard shortcuts

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