javascript 如何批量插入couchdb文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28056083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 08:20:53  来源:igfitidea点击:

How to insert couchdb documents in bulk?

javascriptnode.jsrestcouchdbbulkinsert

提问by Chev

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.

我的任务是修改旧版应用程序,以便用户可以批量上传工资单调整。目前,他们必须填写表格并逐项输入数据,并在每一项之后点击提交。我让他们能够一次上传包含大量调整的 CSV 文件。

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.

沙发 URL 是动态构建的。

req.bodycontains the properties for a single item.

req.body包含单个项目的属性。

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.postcall into a loop as is, but I imagine that's not going to be very performant.

我是沙发新手,但我不确定如何在单个操作中发送多个文档以进行插入。我可以request.post按原样将调用放入循环中,但我想这不会有很好的性能。

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

我只需要指出正确的方向,即可通过其 REST API 批量插入沙发。谢谢!

采纳答案by Dominic Barnes

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

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

回答by KirilStankov

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

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