pymongo光标batch_size一次返回一个文档

发布于 2025-02-07 23:00:30 字数 263 浏览 1 评论 0 原文

我正在尝试查询带有批量尺寸的mongodb。当我将batch_size参数应用于.find()方法时,它返回与不使用批处理大小相同的内容。下面的代码一次返回一个文档,而不是预期的批处理大小。

#MongoDB Connection
...
cursor = collection.find({'Time':{'$gt':58006}}, {'V1':1, 'V2':1}, batch_size=9)
for i in cursor: 
    print(i)

I am trying to query a mongoDB with batch sizes. When I apply the batch_size argument in .find() method, It returns the same thing as using no batch size. The code below returns one document at a time instead of the expected batch size.

#MongoDB Connection
...
cursor = collection.find({'Time':{'$gt':58006}}, {'V1':1, 'V2':1}, batch_size=9)
for i in cursor: 
    print(i)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太阳哥哥 2025-02-14 23:00:30

详细说明了批量大小的使用。

指定从MongoDB实例中每批响应中返回的文档数量。在大多数情况下,修改批处理大小不会影响用户或应用程序,因为Mongosh和大多数驱动程序返回结果,好像MongoDB返回了一批批次。

如果您只想获取 n 文档,请使用 limit()

https://www.mongodb.com/docs/manual/reference/method/cursor.batchSize/ details the use of batch size.

Specifies the number of documents to return in each batch of the response from the MongoDB instance. In most cases, modifying the batch size will not affect the user or the application, as mongosh and most drivers return results as if MongoDB returned a single batch.

If you just want to get n documents, use limit(). https://pymongo.readthedocs.io/en/stable/api/pymongo/cursor.html#pymongo.cursor.Cursor.limit

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文