Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a couchdb document exists using python

Tags:

python

couchdb

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!

like image 786
Oleg Avatar asked Aug 02 '13 07:08

Oleg


1 Answers

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.

like image 140
Kxepal Avatar answered Oct 24 '22 13:10

Kxepal