I'm trying to retrieve random records from my mongodb
collection.
I'm using golang
with mongo-go-driver
pipeline := []bson.E{bson.E{"$sample", bson.E{"size", 10}}}
collection.Aggregate(context.TODO(), pipeline)
The aggregate is returning me this error:
A pipeline stage specification object must contain exactly one field.
I've tried with $size
and size
Is it possible that mongo-go-driver
doesn't support $sample?
Use below instead
pipeline := []bson.D{bson.D{{"$sample", bson.D{{"size", 10}}}}}
bson.D
represents a BSON docoument and bson.E
represents a BSON element. An aggregation is an array of BSON documents. More details can be found at https://godoc.org/go.mongodb.org/mongo-driver/bson.