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

insert and query an OrderedDict in MongoHQ
Return .str of ObjectID using pymongo
Selecting the lowest/highest with find_one in Pymongo
Safe use of Pymongo's find_one
Pymongo $in + $regex
How can I get pymongo to always return str and not unicode?
SON Manipulator in mongo queries
How to append a document to a 2nd level embedded model list document in Django and mongoengine
pymongo multiple update $in
How to get negetive of a complex where clause in mongo db
pymongo justOne & multi
How to make pymongo's find() return a list?
convert pymongo cursor to json
How to do query on pymongo and get values in the reverse order of inserting
How to sort result by field?
advanced query combining or and gte and lt in pymongo

Categories

HOME
xpath
protocol-buffers
libgdx
memory
eclipse-plugin
nsview
bing-search
dronekit-python
moodle-api
greasemonkey
vue-resource
swift2
dji-sdk
jint
repair
haxe
eddystone
siesta
lda
twitter-oauth
pm2
angular4
user-defined-types
gmp
pe
web-analytics
conda
perfino
sipp
vuforia
function-points
data-collection
hdmi
clipboard
openbravo
amazon-cloudtrail
ibm-connections
materialize
region
xenapp
winsock
windows2012
plantuml
cloudera-manager
xmlunit
express-session
pdfnet
praat
stat
icepdf
morphline
non-linear-regression
datediff
pen
popen
http-basic-authentication
wikimapia
msxml
degrees
gapi
tablespace
zynq
right-to-left
deferred-rendering
adp
urbit
xcopy
gulp-uglify
okta-api
flush
phpquery
ibm-was
directx-9
fanotify
gql
modern.ie
angular-amd
rfc5545
configurable-product
flurry-analytics
wicket-1.5
treeline
forio-contour
rails-api
meteor-collections
mft
frontbase
quickgraph
postgres-xc
unicoins
delphi-xe4
jbox2d
proc-open
clearinterval
popup-blocker
arrow
jzmq
java.lang.class
flymake
opends
acpi
pinax
self-tracking-entities
eventaggregator
yahoo-maps
html-components
rawcontacts
bucket
wmd-editor
bindable-linq
rootkit

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