Mongo golang上的随机记录
Mongo random records on golang
我正在尝试从我的 mongodb
collection 中检索随机记录。
我正在使用 golang
和 mongo-go-driver
pipeline := []bson.E{bson.E{"$sample", bson.E{"size", 10}}}
collection.Aggregate(context.TODO(), pipeline)
聚合返回此错误:
A pipeline stage specification object must contain exactly one field.
我试过 $size
和 size
难不成mongo-go-driver
不支持$sample?
改用下面的方法
pipeline := []bson.D{bson.D{{"$sample", bson.D{{"size", 10}}}}}
bson.D
表示一个 BSON 文档,bson.E
表示一个 BSON 元素。聚合是一组 BSON 文档。可以在 https://godoc.org/go.mongodb.org/mongo-driver/bson.
找到更多详细信息
我正在尝试从我的 mongodb
collection 中检索随机记录。
我正在使用 golang
和 mongo-go-driver
pipeline := []bson.E{bson.E{"$sample", bson.E{"size", 10}}}
collection.Aggregate(context.TODO(), pipeline)
聚合返回此错误:
A pipeline stage specification object must contain exactly one field.
我试过 $size
和 size
难不成mongo-go-driver
不支持$sample?
改用下面的方法
pipeline := []bson.D{bson.D{{"$sample", bson.D{{"size", 10}}}}}
bson.D
表示一个 BSON 文档,bson.E
表示一个 BSON 元素。聚合是一组 BSON 文档。可以在 https://godoc.org/go.mongodb.org/mongo-driver/bson.