pymongo


Safe use of Pymongo's find_one


Whats the behavior of Pymongo find_one? I would expect such a function to return a None or throw an exception when it does not find the required document. But it behaves like:
>>> q = db.find_one({'node_type': {'$regex':'impossible-condition'}})
>>> q
>>>
>>> q==1
False
>>> w==1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'w' is not defined
Does it return anything? And how to safely determine if the query did not match a value?
Since the find_one is the collection level operation, you need to actually provide collection that you are querying against. Taking your example, we will look at the users collection:
q = db.users.find_one({'node_type': {'$regex':'impossible-condition'}})
After that the following applies: find_one returns single document, or None if no matching document is found.
See:
http://api.mongodb.org/python/current/api/pymongo/collection.html#pymongo.collection.Collection.find_one

Related Links

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

Categories

HOME
ionic2
generics
apple-push-notifications
openlayers-3
liferay-7
dependencies
elk-stack
jint
bittorrent
u-boot
swarm
log4j2
driver
createjs
stimulsoft
spring-ldap
acl
pyopencl
pubxml
password-generator
plyr
window-managers
multi-dimensional-scaling
uri
jackrabbit-oak
gravity-forms-plugin
bobo-browse.net
fusion
mapnik
opnet
actframework
android-alarms
recovery
tidal-scheduler
reset
imgur
pyttsx
access-denied
vungle-ads
type-inference
wysihtml5
sonarqube5.2
tinyioc
libreoffice-writer
ol3-google-maps
hibernate-ogm
sharpdx
mobile-angular-ui
wikimapia
ogg
metabase
xcode6.4
msg
livereload
livecycle
p6spy
traversable
cdo.message
android-sdcard
node.js-connect
qpixmap
noclassdeffounderror
exists
processor
django-redis
okta-api
largenumber
lowercase
dsa
bjam
gitlab-omnibus
wlanapi
domino-designer-eclipse
kefir.js
baseadapter
nanomsg
phonegap-facebook-plugin
microsoft-expression-web
mobile-country-code
webmethod
incognito-mode
commenting
fileshare
smips
jmenubar
eclim
mp4parser
ax
bcdedit
jquery-lazyload
web-frameworks
xalan
clearinterval
prototypal-inheritance
kolite
n-queens
startupscript
kernel32
simile
ognl
servlet-container
gtktextview
regioninfo
rendering-engine
rootkit

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