I need to fetch couchdb documents by a bunch of ids. Is there an request / API to do it ? I don't want to create a view (id, docs) and then do a find by keys. when the id b-tree already exists

有帮助吗?

解决方案

You should use the bulk API documented here.

It could look something like below.

  • Pass in the keys as part of the body via a POST
  • Pass in the name of the db (in the example below, it's demodb)
  • in the url, use the _all_docs for the view
  • if you want the entire documents returned (and not just the rev), be sure to pass include_docs=true

curl -d '{"keys":["docId1","docId2","docId3"]}' -X POST http://127.0.0.1:5984/demodb/_all_docs?include_docs=true

其他提示

If you want to fetch a range of documents, you can also use startkey_docid and limit parameters like you would in a view.

What you do is a GET request to a URL like

http://127.0.0.1:5984/demodb/_all_docs?startkey_docid="docId1"&limit=5

Once you have your set of results, you can use the last returned it as the next startkey and run the request again. This has the benefit of skipping view indexing processes (which can be a pain in the ass for large databases).

Documented in the CouchDB API, here.

If you're using the python-couchdb library, you can use:

_, _, response = <your_db>.resource.post_json('_bulk_get', {'docs': [{'id': '<1st_doc_id>'}, {'id': '<2nd_doc_id>'}]})

Your results will be in response['results'].

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top