I'm trying to execute a query with "sort" and "limit". With mgo you could do Find(nil).Sort(“-when”).Limit(10) but the new, official mongo driver has no such methods. How can I sort and "limit" with the new driver?
6 Answers
In the current version mongo-go-driver v1.0.3, the options are simplified. For example to perform find, sort and limit:
import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
options := options.Find()
// Sort by `_id` field descending
options.SetSort(bson.D{{"_id", -1}})
// Limit by 10 documents only
options.SetLimit(10)
cursor, err := collection.Find(context.Background(), bson.D{}, options)
See more available options on godoc.org/go.mongodb.org/mongo-driver/mongo/options. Especially FindOptions for all possible options for Find().
Comments
The official driver is not straightforward as mgo. You can do sort and limit using the findopt.Limit and findopt.Sort.
You can see examples from the official repository.
2 Comments
options.Find() instead.You can use
findOptions := options.Find()
findOptions.SetLimit(2)
findOptions.SetSkip(2)
...
cursor, err := collection.Find(context.Background(), bson.M{}, findOptions)
resource at https://www.mongodb.com/blog/post/mongodb-go-driver-tutorial
Comments
you need import "github.com/mongodb/mongo-go-driver/options" package to build a findOptions.
import github.com/mongodb/mongo-go-driver/options
findOptions := options.Find() // build a `findOptions`
findOptions.SetSort(map[string]int{"when": -1}) // reverse order by `when`
findOptions.SetSkip(0) // skip whatever you want, like `offset` clause in mysql
findOptions.SetLimit(10) // like `limit` clause in mysql
// apply findOptions
cur, err := collection.Find(context.TODO(), bson.D{}, findOptions)
// resolve err
for cur.Next(context.TODO()) {
// call cur.Decode()
}
Comments
The sort-option apparently requires you to add a map[string]interface{} where you can specify a field as key and a sortOrder as value (where 1 means ascending and -1 means descending) as following:
sortMap := make(map[string]interface{})
sortMap["version"] = 1
opt := findopt.Sort(sortMap)
As far as I can see this means that you are only able to properly sort results by one sortField because keys in a go map are stored in a random order.
Comments
ONE LINE OPTION
I know there is already a lot of answers but you can do it as one line (if you need it for any case of yours)
// From the Doc
// func (f *FindOptions) SetSort(sort interface{}) *FindOptions
cursor, err := collection.Find(context.Background(), bson.M{}, options.Find().SetSort(map[string]int{"when": -1}).SetLimit(10))
SetSort() and the others currently returns the parent pointer itself
findopt.Sortto build options forFind. Unfortunately, it takes aninterface{}argument, the documentation says nothing useful, and there are no examples in the documentation or GitHub.