douwan2664 2019-01-09 20:09
浏览 597


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; doc != null; doc = await {
    if (doc._id % divisor === 0) {

I have something like this so far:

err := c.Find(nil).Batch(1).All(&items)

But it's not working like expected.


  • 写回答

1条回答 默认 最新

  • dongren7374 2019-01-10 03:51

    The batchSize() specifies the number of documents to return in each batch of the response. You will still all results. For example, if the total number of returned docs is 100 and you specify batch size as 20, you will have 1 find and 4 getMore commands sent from the client to the mongod to complete the request.

    One little trick you can use to verify. Use db.setProfilingLevel(0, 0) from mongo shell to log everything in the log file. Execute your test app and you will see find and getMore recorded in the log.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥20 vscode虚拟环境依赖包未安装
  • ¥15 odoo17关于owl开发js代码问题
  • ¥15 光纤中多普勒频移公式的推导
  • ¥15 怎么制作一个人脸识别门禁系统
  • ¥20 大华dss监控平台网络关闭登不进去
  • ¥15 请使用蚁群算法解决下列问题,并给出我完整的代码
  • ¥20 关于php录入完成后,批量更新数据库
  • ¥15 请教往复密封润滑问题
  • ¥15 cocos creator发布ios包
  • ¥15 comsol压电材料数据