ChevCast
ChevCast

Reputation: 59213

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.

Upvotes: 2

Views: 15910

Answers (2)

KirilStankov
KirilStankov

Reputation: 87

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

Upvotes: 0

Dominic Barnes
Dominic Barnes

Reputation: 28429

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

Upvotes: 4

Related Questions