pymongo


python motor mongo cursor length or peek next


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 ","
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

Related Links

How to enable `projection` in pymongo query class `find()`?
Variables don't hold value for very long pymongo ipython
pymongo upgrade to 3.0.3 causes this statement to fail
UUID to NUUID in Python
how to test for empty mongodb query in pymong
read video file from mongodb with pymongo
Get OperationFailure error with PyMongo 2.7.1
Inserting string as regular string in mongodb
Mongos + Pymongo 2.5 ==>No suitable hosts found
pymongo collection add item using update
How to convert objectid to string
Conditional atomic mongo remove with different results
python script to connect to mongodb master
flask-admin and pymongo search
Running code when mongoengine loads a document
Can't replace mongo document

Categories

HOME
qt
elasticsearch
oracle
webview
shinyapps
jms
livecode
selenium-builder
antd
foaf
loopbackjs
ant
nsis
tcp
cryptography
elisp
greasemonkey
flurry
angular-meteor
tweepy
swarm
twitter-oauth
fbloginview
documentum
bootstrap-switch
google-awareness
xbox360
vmware-workstation
exe
office-ui-fabric
coroutine
error-logging
akka-persistence
altium-designer
uislider
fluent-migrator
environment
web-config-transform
msdeploy
tf-idf
ccavenue
bonita
ninject
microprocessors
jsonresult
argv
reporting
xcrun
actframework
private-key
image-optimization
custom-keyboard
pdfminer
spinner
cordys-opentext
llvm-ir
cheat-engine
grails3.2.0
angular-datatables
opal-framework
coverage.py
transparency
daz3d
common-table-expression
rm
django-static-precompiler
xcglogger
redisson
simple-schema
google-news
jtag
node.js-connect
jeasyui
psd
jsr363
seadragon
ajax4jsf
miniconda
bjam
wlanapi
uitest
modern.ie
angular-amd
findersync
baseadapter
image-rotation
tabris
stripe.net
rails-api
bittorrent-sync
boolean-algebra
jms-serializer
progress-db
boinc
yii-booster
jquery-transit
libxml-js
tnt4j
delphi-xe4
cmath
spark-view-engine
live-wallpaper
voice-recording
word-frequency
cakeyframeanimation
struts2-s2hibernate
kohana-auth
dip
libxslt
resharper-5.1
odac
regioninfo

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App