简体   繁体   中英

How to insert couchdb documents in bulk?

I'm tasked with modifying a legacy app so that users can upload payroll adjustments in bulk. Currently they have to fill out a form and input the data item by item, hitting submit after each one. I'm giving them the ability to upload a CSV file containing tons of adjustments at once.

On the server they are inserting items into couch one by one, like this:

function wsapiPOST(req, res, next) {
  var path = req.path.substr(6)
    , url = couchPath + path + requestUtil.buildQueryString(req);

  request.post({
    url: url,
    headers: { 'content-type': 'application/json' },
    body: JSON.stringify(req.body)
  },function (err, resp, body) {
    if (err) {
      if (resp) {
        res.writeHead(resp.statusCode);
        res.end(body);
      } else { // This would happen if the request timed out
        res.writeHead(408);
        res.end('timeout');
      }
    }
  }).pipe(res);
}

The couch URL is built dynamically.

req.body contains the properties for a single item.

I'm new to couch but I'm not sure how to send multiple documents for insertion in a single operation. I could throw the request.post call into a loop as is, but I imagine that's not going to be very performant.

I just need pointed in the right direction for bulk insertion into couch via its REST API. Thanks!

您可以使用批量文档API插入(甚至更新)多个文档。

您可以使用nano-有批量插入选项。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM