最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to insert CouchDB documents in bulk? - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question edited Apr 25, 2023 at 11:14 halfer 20.3k19 gold badges109 silver badges202 bronze badges asked Jan 20, 2015 at 22:10 ChevCastChevCast 59.3k66 gold badges221 silver badges325 bronze badges 1
  • Can you just send an array to the rest api? – Vsevolod Goloviznin Commented Jan 20, 2015 at 22:16
Add a ment  | 

2 Answers 2

Reset to default 4

You can use the bulk document API to insert (and even update) multiple documents.

You can use nano - there is a bulk insert option.

发布评论

评论列表(0)

  1. 暂无评论