I was wondering if there is a way to check if a document with a particular ID exists in couchdb using couch python library. It seems that if I do this:
server = couchdb.Server('http://localhost:5984')
db = server['tweets']
mytemp = db[MyDocId]
and the document doesn't exist, the code throws a "ResourceNotFound" exception.
I could just catch the exception and put my code in the exception handling portion, but it seems too dirty.
I was hoping there is a way to have an "if" statement that checks whether a document with a particular key exists or not.
Thanks!
The database object mimics to dict api, so it's very simple and native to check for docs in database:
server = couchdb.Server('http://localhost:5984')
db = server['tweets']
if MyDocId in db:
mytemp = db[MyDocId]
mytemp = db.get(MyDocId)
if mytemp is None:
print "missed"
See couchdb-python docs for more.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With