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 do I deal with Error stated below?
pymongo query return in direct query not in pymongo
Displaying nested dictionaries in flask-admin
mongoengine slow serialization of embedded documents with reference fields
Pymongo error : 'Cursor' object has no attribute 'find' , how to filter it?
Spark Streaming: foreachRDD insert into mongoDB using python?
how to find right version of bson from pip for pymongo/mongoengine
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

Categories

HOME
hpoo
oracle
powerbi
shinyapps
angular-ui-grid
qc
redmine
dry
nsview
ews
adobe
consul
amp
implicit-conversion
minecraft
driver
fireloop
parsley.js
mpu6050
squirrel-sql
trace32
supervisord
tomcat8
multiple-tables
parceler
social-tables
activeadmin
neo4j.rb
candlestick-chart
fstar
adapter
ibm-connections
splunk-query
substance
argv
wampsharp
ms-media-foundation
payeezy
db2-luw
xerces-c
gitolite
icepdf
secure-coding
raytracing
fluid-mac-app-engine
git-tfs
jenkins-slave
brightscript
entropy
winmerge
patternlab.io
dtd
sesame
predicates
pecl
fitbit
haraka
rational-performance-test
msg
jsonix
data-management
p6spy
fiber
meld
deferred-rendering
exists
sharepoint-apps
android-async-http
certificate-authority
android-mapview
smart-tv
decidable
arbre
srv-record
calcite
wso2ml
search-form
artisan
phpredis
mgwt
debug-symbols
mod-auth-openidc
uno
lifetime
loginview
otl
communicate
forio-contour
fileshare
cidr
apportable
xs
spiral
ui-design
onconfigurationchanged
gitx
coordinate
telerik-ajax
image-scanner
getopts
qt-mobility
botnet
photo-management
dip
hungarian-notation
sscli
reliability
.net-client-profile
prism-2
bindable-linq
account-management

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App