[ACCEPTED]-Mongo conditional for "key doesn't exist"?-key-value

Accepted answer
Score: 29

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
Score: 19

You can test for a key not existing with:

db.myDocs.find_one({'myKey': { '$exists': False }})

Mongo 1 documentation about the $exists operator

More Related questions