Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在使用 pymongo 从 MongoDB 插入和检索数据。这两个操作可以同时进行。问题是当我rows = db.<collection>.find()在 pymongo 中进行操作时,每次都会rows.count()返回不同的响应(因为数据的插入也在同时进行)。有什么方法可以限制 MongoDB 只返回我执行find()语句时存在的行?我尝试添加snapshot=True,find()但问题仍然存在。
rows = db.<collection>.find()
rows.count()
find()
snapshot=True
db.<collection>.find().count()无论如何都会对 mongodb 进行额外的 ( runCommand count) 调用。
db.<collection>.find().count()
runCommand
count
如何简单地获取光标的长度,如下所示:
rows = db.<collection>.find() print len(list(rows))
请注意,您不能只使用len(rows).
len(rows)
希望有帮助。