python motor mongo cursor length或peek next(python motor mongo cursor length or peek next)

编程入门 行业动态 更新时间:2024-10-26 06:27:39
python motor mongo cursor length或peek next(python motor mongo cursor length or peek next)

有没有办法确定电机mongo光标的长度或向前偷看以查看是否有下一个(而不是fetch_next或许has_next )

而不是没有进入提供的限制的cursor.size() ()

基本上我想添加所需的json逗号

while (yield cursor.fetch_next): document = cursor.next_object() print document if cursor.has_next() # Sweeet print ","

is there a way of determining the length of the motor mongo cursor or peeking ahead to see if there is a next ( instead of fetch_next perhaps has_next )

and not the cursor.size() that does not take into the provided limit()

basically i desire to add the required json comma

while (yield cursor.fetch_next): document = cursor.next_object() print document if cursor.has_next() # Sweeet print ","

最满意答案

您可以使用“alive”属性。 试试这个:

from tornado import gen, ioloop import motor client = motor.MotorClient() @gen.coroutine def f(): collection = client.test.collection yield collection.drop() yield collection.insert([{'_id': i} for i in range(100)]) cursor = collection.find() while (yield cursor.fetch_next): print cursor.next_object(), cursor.alive ioloop.IOLoop.current().run_sync(f)

它打印“True”直到最终文档,当alive为“False”时。

MotorCursor批量从服务器获取数据。 ( 关于批处理的MongoDB文档解释了游标和批处理如何适用于所有MongoDB驱动程序,包括Motor。)当“alive”为True时,表示服务器上有更多可用数据,或者数据在MotorCursor中缓冲,或者两者都有。

然而,有一种竞争条件。 假设您获取除最终文档之外的所有文档,并且在获取最后一个文档之前,另一个客户端删除它,那么即使“alive”为“True”,您也将无法找到最后一个文档。 最好重新安排你的循环:

@gen.coroutine def f(): collection = client.test.collection yield collection.drop() yield collection.insert([{'_id': i} for i in range(100)]) cursor = collection.find() if (yield cursor.fetch_next): sys.stdout.write(str(cursor.next_object())) while (yield cursor.fetch_next): sys.stdout.write(", ") sys.stdout.write(str(cursor.next_object())) print

You can use the "alive" property. Try running this:

from tornado import gen, ioloop import motor client = motor.MotorClient() @gen.coroutine def f(): collection = client.test.collection yield collection.drop() yield collection.insert([{'_id': i} for i in range(100)]) cursor = collection.find() while (yield cursor.fetch_next): print cursor.next_object(), cursor.alive ioloop.IOLoop.current().run_sync(f)

It prints "True" until the final document, when alive is "False".

A MotorCursor fetches data from the server in batches. (The MongoDB documentation on batches explains how cursors and batches work for all MongoDB drivers, including Motor.) When "alive" is True it means either that there is more data available on the server, or data is buffered in the MotorCursor, or both.

There is a race condition, however. Say that you fetch all but the final document, and before you fetch that last document another client deletes it, then you'll fail to find the last document even though "alive" was "True". Better to rearrange your loop:

@gen.coroutine def f(): collection = client.test.collection yield collection.drop() yield collection.insert([{'_id': i} for i in range(100)]) cursor = collection.find() if (yield cursor.fetch_next): sys.stdout.write(str(cursor.next_object())) while (yield cursor.fetch_next): sys.stdout.write(", ") sys.stdout.write(str(cursor.next_object())) print

更多推荐

本文发布于:2023-08-07 05:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1462931.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:mongo   motor   python   peek   length

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!