Search code examples
ibm-cloudcloudantnode-red

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


I have a function node that creates an array; is there a way to store all the entries in a Cloudant database?

Thanks.


Solution

  • Here is a sample flow using the http request node, based on 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":[]}]
    

    enter image description here