Question

How to fetch multiple documents from CouchDB, in particular with couchdb-python?

Was it helpful?

Solution 3

import couchdb
import simplejson as json

resource = couchdb.client.Resource(None, 'http://localhost:5984/dbname/_all_docs')
params = {"include_docs":True}
content = json.dumps({"keys":[idstring1, idstring2, ...]})
headers = {"Content-Type":"application/json"}
resource.post(headers=headers, content=content, **params)
resource.post(headers=headers, content=content, **params)[1]['rows']

OTHER TIPS

Easiest way is to pass a include_docs=True arg to Database.view. Each row of the results will include the doc. e.g.

>>> db = couchdb.Database('http://localhost:5984/test')
>>> rows = db.view('_all_docs', keys=['docid1', 'docid2', 'missing'], include_docs=True)
>>> docs = [row.doc for row in rows]
>>> docs
[<Document 'docid1'@'...' {}>, <Document 'docid2'@'...' {}>, None]

Note that a row's doc will be None if the document does not exist.

This works with any view - just provide a list of keys suitable to the view.

This is the right way:

import couchdb

server = couchdb.Server("http://localhost:5984")
db = server["dbname"]
results = db.view("_all_docs", keys=["key1", "key2"])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top