pymongo


Variables don't hold value for very long pymongo ipython


Perhaps I should just restart my computer, but it seems my variable are losing their values. A simple aggregation only seems to hold the contents of my database for a short period of time. Note: I'm doing this in an ipython notebook.
MONGODB_URI ='mongodb://username:password#***.mongolab.****/***'
client = MongoClient(MONGODB_URI)
db = client.get_default_database()
collectn = db.collection_name
pipe = [
{"$unwind":"$predictions"},
{"$match": {"predictions.t_obj": datetime.datetime(2015, 10, 29, 11, 0)}}
]
should_be_data = collectn.aggregate(pipe)
list(should_be_data)
// returns what we expect, i.e. data
list(should_be_data)
// returns []
Why do the contents of my variable disappear?
should_be_data isn't a list/data-container, but a generator.
The first time you run list(should_be_data), the generator is consumed completely. This lines consumed the elements from the generator, and stores them in a new list.
By the second time you run list(should_be_data), the generator is already exhaused, thus it returns no further elements.
If you want it to be a list to begin with, just replace
should_be_data = collectn.aggregate(pipe)
with
should_be_data = list(collectn.aggregate(pipe))

Related Links

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
How do I call a function with pymongo using webpy?
python motor offers fetch_next exception on aggregation
python motor mongo cursor length or peek next
PyMongo check if update succeeded
insert and query an OrderedDict in MongoHQ
Return .str of ObjectID using pymongo

Categories

HOME
xpath
office365
spring-data
android-studio
windows-8.1
apk
malware
google-sheets-query
gprs
firebase-database
native-base
lc3
scrape
getopenfilename
parsley.js
freertos
squirrel-sql
distributed-computing
fresco
exec-maven-plugin
salt-cloud
deepfreeze
error-logging
android-permissions
amazon-sns
morris.js
background-color
offline-caching
marquee
fatfs
envoy
guice
locks
google-play-developer-api
universal
seafile-server
pdfminer
bluemixtools
cordys-opentext
twilio-php
gitolite
sbjson
stat
utf
web-inspector
cloudwatch
webalizer
hibernate-ogm
pen
gnome-terminal
glassfish-4.1
lvalue
alertify
dymola
google-shopping-api
wildfly-9
email-injection
gawk
boost-compute
mathjs
gcloud-node
coovachilli
dojo-build
verisign
preferenceactivity
okta-api
android-expansion-files
grinder
odesk
nette
ambiguity
supercomputers
unhandled
cache-manifest
webproject
scala-swing
sablecc
berkeley-db-xml
relationships
sqlbindparameter
boolean-algebra
map
jmenubar
rautomation
disjoint-union
aero
oscilloscope
ofstream
contextswitchdeadlock
simplecov
idispatch
struts2-json-plugin
audiotoolbox
android-dialog
uipangesturerecognizer
getopts
rtd
upshot
suphp
ninject-interception
dm
tacit-programming
accumulator
ntruencrypt
suggestbox
community-server

Resources

Encrypt Message