检查 mongo-go-driver 中的接口 Cursor:
https://github.com/mongodb/mongo-go-driver/blob/master/mongo/cursor.go#L37
没有Limit
或Skip
功能。
如何对结果进行分页?
我想我在尝试Sort
or时会遇到同样的问题Count
。
有办法吗?或者,这根本还没有在官方驱动程序中实现?
检查 mongo-go-driver 中的接口 Cursor:
https://github.com/mongodb/mongo-go-driver/blob/master/mongo/cursor.go#L37
没有Limit
或Skip
功能。
如何对结果进行分页?
我想我在尝试Sort
or时会遇到同样的问题Count
。
有办法吗?或者,这根本还没有在官方驱动程序中实现?
您可以在包https://github.com/mongodb/mongo-go-driver/tree/master/mongo/options中检查大多数查找选项options
client, err := mongo.Connect(context.Background(), "mongodb://localhost:27017", nil)
// check err
db := client.Database("examples")
coll := db.Collection("inventory")
{
cursor, err := coll.Find(
context.Background(),
options.SetSort(bson.NewDocument(bson.EC.Int64("x", 1))),
options.SetLimit(30),
options.SetSkip(5),
)
// cursor decode...
}
用过滤器计数
count, err :=coll.Count(context.Background(),bson.NewDocument(bson.EC.String("foo", "bar")))
从文档元数据计数
count, err := coll.EstimatedDocumentCount(context.Background(),countopt.MaxTimeMs(100))
编辑:
Mongo-go-driver stable v1.0.0 发布,分离BSON库,请参考官方文档