[ACCEPTED]-Mongo conditional for "key doesn't exist"?-key-value
Accepted answer
For "if key exists" checks, using 1 a .find()
is significantly faster than find_one()
.
Single document: cursor = db.myDocs.find({"mykey": {"$exists": True}}).limit(1)
Multiple documents: cursor = db.myDocs.find({"mykey": {"$exists": True}})
if cursor.count() > 0:
keyExists = True
else:
keyExists = False
You can test for a key not existing with:
db.myDocs.find_one({'myKey': { '$exists': False }})
Mongo 1 documentation about the $exists operator
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.