Documentation ¶
Overview ¶
Package minquery provides a mgo-like Query type called MinQuery, which supports efficient query pagination (cursors to continue listing documents where we left off).
Example using MinQuery ¶
Let's say we have a users collection in MongoDB modeled with this Go struct:
type User struct { ID bson.ObjectId `bson:"_id"` Name string `bson:"name"` Country string `bson:"country"` }
To query users having country=USA, sorted by Name and ID:
q := minquery.New(session.DB(""), "users", bson.M{"country" : "USA"}). Sort("name", "_id").Limit(10) // If this is not the first page, set cursor: // getLastCursor() represents your logic how you acquire the last cursor. if cursor := getLastCursor(); cursor != "" { q = q.Cursor(cursor) } var users []*User newCursor, err := q.All(&users, "country", "name", "_id")
And that's all. newCursor is the cursor to be used to fetch the next batch.
Note #1: When calling MinQuery.All(), you have to provide the names of the cursor fields, this will be used to build the cursor data (and ultimately the cursor string) from.
Note #2: If you're retrieving partial results (by using MinQuery.Select()), you have to include all the fields that are part of the cursor (the index entry) even if you don't intend to use them directly, else MinQuery.All() will not have all the values of the cursor fields, and so it will not be able to create the proper cursor value.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultCursorCodec cursorCodec
DefaultCursorCodec is the default CursorCodec value that is used if none is specified. The default implementation produces web-safe cursor strings.
var DefaultCursorCodecMongo cursorCodecMongo
var UseMongoDriver = false
Functions ¶
This section is empty.
Types ¶
type CursorCodec ¶
type CursorCodec interface { // CreateCursor returns a cursor string from the specified fields. CreateCursor(cursorData bson.D) (string, error) // ParseCursor parses the cursor string and returns the cursor data. ParseCursor(c string) (cursorData bson.D, err error) }
CursorCodec represents a symmetric pair of functions that can be used to convert cursor data of type bson.D to a string and vice versa.
type CursorCodecMongo ¶ added in v1.1.16
type CursorCodecMongo interface { CreateCursorMongo(cursorData mongoDriverBson.D) (string, error) ParseCursorMongo(c string) (cursorData mongoDriverBson.D, err error) }
type MinQuery ¶
type MinQuery interface { // Sort asks the database to order returned documents according to // the provided field names. Sort(fields ...string) MinQuery // Select enables selecting which fields should be retrieved for // the results found. Select(selector interface{}) MinQuery // Limit restricts the maximum number of documents retrieved to n, // and also changes the batch size to the same value. Limit(n int) MinQuery // Cursor sets the cursor, which specifies the last index entry // that was already returned, and result documents will be listed after this. // Parsing a cursor may fail which is not returned. If an invalid cursor // is specified, All() will fail and return the error. Cursor(c string) MinQuery // CursorCoded sets the CursorCodec to be used to parse and to create cursors. // This gives you the possibility to implement your own logic to create cursors, // including encryption should you need it. CursorCodec(cc CursorCodec) MinQuery // All retrieves all documents from the result set into the provided slice. // cursorFields lists the fields (in order) to be used to generate // the returned cursor. All(result interface{}, cursorFields ...string) (cursor string, err error) }
MinQuery is an mgo-like Query that supports cursors to continue listing documents where we left off. If a cursor is set, it specifies the last index entry that was already returned, and result documents will be listed after this.