dnolen
dnolen

Reputation: 18556

Multiple CouchDB Document fetch with couchdb-python

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

Upvotes: 12

Views: 6320

Answers (3)

Matt Goodall
Matt Goodall

Reputation: 1682

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.

Upvotes: 22

Anand Chitipothu
Anand Chitipothu

Reputation: 4367

This is the right way:

import couchdb

server = couchdb.Server("http://localhost:5984")
db = server["dbname"]
results = db.view("_all_docs", keys=["key1", "key2"])

Upvotes: 4

dnolen
dnolen

Reputation: 18556

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']

Upvotes: -7

Related Questions