Bluemix NodeRed - 如何在 Cloudant DB 中一次存储多个文档?

Bluemix NodeRed - How can I store multiple documents at one time in Cloudant DB?

我有一个 function 节点创建一个数组;有没有办法将所有条目存储在 Cloudant 数据库中?

谢谢。

看起来 Node-RED 只支持 Cloudant 中的基本插入或删除操作 - 这意味着不支持 _bulk_docs 端点 (docs here)。

您可以使用 HTTP 请求功能通过 API 发出您自己的 _bulk_docs 请求,使用上面链接的文档,这应该允许您一次插入多个文档。

如果您保留数组,您可以使用类似拆分节点的东西将其分解成单独的消息并一次插入 1 个。

或者更改您的功能节点以输出消息流并再次一次插入它们。

这是一个使用 http 请求节点的示例流程,基于 https://docs.cloudant.com/document.html#bulk-operations

[{"id":"b97b6381.90cd18","type":"inject","z":"b4aeef03.c16228","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":95.5,"y":330,"wires":[["accfba5c.0df688"]]},{"id":"accfba5c.0df688","type":"function","z":"b4aeef03.c16228","name":"Multiple records to insert","func":"msg.payload = {\n  \"docs\": [\n    {\n      \"name\": \"Nicholas\",\n      \"age\": 45,\n      \"gender\": \"female\"\n    },\n    {\n      \"name\": \"Taylor\",\n      \"age\": 50,\n      \"gender\": \"female\"\n    }\n  ]\n}\n\nmsg.headers={\"Content-Type\":\"application/json\"}\n\nreturn msg;","outputs":1,"noerr":0,"x":357.9000244140625,"y":331.5999755859375,"wires":[["a950d685.dcf92"]]},{"id":"a950d685.dcf92","type":"http request","z":"b4aeef03.c16228","name":"Bulk add to Cloudant","method":"POST","ret":"txt","url":"https://REPLACE-bluemix.cloudant.com/dev/_bulk_docs","x":636.9000244140625,"y":330.79998779296875,"wires":[["e2355dc3.e1118"]]},{"id":"e2355dc3.e1118","type":"debug","z":"b4aeef03.c16228","name":"","active":true,"console":"false","complete":"false","x":888.9000244140625,"y":329.79998779296875,"wires":[]}]