How can I get the fields for a couchdb document? I'm thinking of how to use couchdb-python, and often will not know the complete set of fields for a document. I haven't seen anything about how to introspect a document in the docs. What's the best way? If the document was a python object I would query object.__dict__
for its attributes.
views:
58answers:
1
A:
.keys() returns a list of field names.
db = server['test']
for doc in db:
print doc
for key in db[doc].keys():
print key
You should get the doc id, followed by the fields.
Philip T.
2010-06-19 18:17:19
Documents as returned by CouchDB are just plain old dictionaries with a couple of extra properties. You use them just like you would any other dict.
LeafStorm
2010-06-20 02:49:02
Thanks -- so is db[doc] a python dict? Sorry for the naivety, I haven't had a chance to install couchdb yet to play with it.
ricopan
2010-06-20 03:47:49
Yeah, it is a dict. I've only been playing with it a bit myself, while learning Python.
Philip T.
2010-06-20 05:18:40