Multiple CouchDB Document fetch with couchdb-python

前端 未结 3 1185
广开言路
广开言路 2020-12-31 10:16

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

相关标签:
3条回答
  • 2020-12-31 10:41

    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.

    0 讨论(0)
  • 2020-12-31 10:45

    This is the right way:

    import couchdb
    
    server = couchdb.Server("http://localhost:5984")
    db = server["dbname"]
    results = db.view("_all_docs", keys=["key1", "key2"])
    
    0 讨论(0)
  • 2020-12-31 10:47
    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']
    
    0 讨论(0)
提交回复
热议问题