How can one fetch records in batches from a mongo database using golang? I know mongoDB itself has something called cursor.batchSize(), but im trying to find an example using a golang driver. From what I've seen the golang library mgo has a function called Batch, but Im trying to figure out how to us it.
Ideally I'm looking for something like this:
const cursor = useDb.collection(mycollection).find().batchSize(10000);
for (let doc = await cursor.next(); doc != null; doc = await cursor.next()) {
if (doc._id % divisor === 0) {
counter++;
}
}
I have something like this so far:
err := c.Find(nil).Batch(1).All(&items)
But it's not working like expected.
Links: https://docs.mongodb.com/manual/reference/method/cursor.batchSize/ https://godoc.org/github.com/globalsign/mgo#Query.Batch