Documentation
¶
Overview ¶
Package mqb creates mgo queries from HTTP requests.
Let's say you have a collection represented by the following type:
type Person struct {
Name string
Age int
}
then this package creates from a http request with parameters like:
/?name=peter&age=10&field=name&limit=10&offset=2&sort=-name&sort=age
a mgo query like:
s := session.DB("dbname")
q := s.C("people").Find(bson.M{"name": bson.RegExp{Pattern: "peter", Options: ""}, "age": 10}).Select(bson.M{"name": 1}).Limit(10).Skip(2).Sort("-name", "age")
Index ¶
Constants ¶
This section is empty.
Variables ¶
var (
DefaultPageSize uint = 20 // DefaultPageSize defines how many elements a page contains per default.
)
Functions ¶
This section is empty.
Types ¶
type MongoQuery ¶
type MongoQuery struct {
// contains filtered or unexported fields
}
MongoQuery can be used to to create mgo.Query from http request parameters.
func NewMongoQuery ¶
func NewMongoQuery(endPointStruct interface{}, database *mgo.Database) *MongoQuery
NewMongoQuery returns a new MongoQuery.
func (*MongoQuery) AddOrOverwriteValidParameter ¶
func (mq *MongoQuery) AddOrOverwriteValidParameter(name string, value reflect.Kind)
AddOrOverwriteValidParameter adds or overwrites a valid parmeter with name and reflect.Kind.
func (*MongoQuery) CreateQuery ¶
CreateQuery creates a mgo.Query from a HTTP Request for a collection represented by endpointStruct.
Examples:
mq := NewMongoQuery(People{}, db)
q, _ := mq.CreateQuery(req) // creates a query from the request for the people collection
mq.DisableParameters("name", "sort")
q, _ := mq.CreateQuery(req) // creates a query from the request for the people collection with the parameters "name" and "sort" disabled.
func (*MongoQuery) DisableParameters ¶
func (mq *MongoQuery) DisableParameters(paramters ...string)
DisableParameters disables paramters. If a URL query contains any of those paramters, an error is returned.
type Page ¶
type Page struct {
Size uint `json:"size"` // Size defines how many elements a page contains.
Items uint `json:"items"` // Items defines the total number of items the corresponding query returns.
Last uint `json:"last"` // Last represents total number of pages a query generates (depends on the page size and the total number of elements returned by the query).
Current uint `json:"current"` // Current is the current page nuber for the query.
}
Page the paging information.