Domanda

Come recuperare più documenti da CouchDB, in particolare con couchdb-python?

È stato utile?

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

Altri suggerimenti

Il modo più semplice è passare un include_docs = True arg a Database.view. Ogni riga dei risultati includerà il documento. per es.

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

Nota che il documento di una riga sarà Nessuno se il documento non esiste.

Funziona con qualsiasi vista: basta fornire un elenco di chiavi adatte alla vista.

Questo è il modo giusto:

import couchdb

server = couchdb.Server("http://localhost:5984")
db = server["dbname"]
results = db.view("_all_docs", keys=["key1", "key2"])
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top