Arangodb: Insert a list of documents using Foxx Api

Created on 31 Aug 2016  路  4Comments  路  Source: arangodb/arangodb

I Testing "post a document" using Foxx according of Arangodb foxx docs.

router.post('/entries', function (req, res)

it's fine worked for insert a document.
now, how can I insert a list of documents into my collection using Foxx?

1 Question 3 Foxx

Most helpful comment

In ArangoDB 3.0 Foxx collections are just plain ArangoDB collections so the usual collection API is available on them: https://docs.arangodb.com/3.0/Manual/Appendix/References/CollectionObject.html

Your code does the trick, but it seems a bit redundant to create a copy of the request body as it's already a JS array. You can also use ES6-style for-of loops to make the code a bit more readable:

function (req, res) {
  for (var data of req.body) {
    foxxColl.save(data);
  }
}

All 4 comments

You simply post a list of documents to that endpoint, and iterate over them in the handler.

@dothebart: thanks... well, finally I Inserted with below Api:

router.post('/myColls', function (req, res) {
const data = Array.prototype.slice.call(req.body);
for (var i = 0; i < data.length; i++) {
foxxColl.save(data[i]);
};
})
.body(joi.array().required(), 'Entries to store in the collection.')
.summary('Store entries')
.description('Stores entries in the "myColl" collection.');

it's worked!, but I want to know this way is good? or exists better way?

In ArangoDB 3.0 Foxx collections are just plain ArangoDB collections so the usual collection API is available on them: https://docs.arangodb.com/3.0/Manual/Appendix/References/CollectionObject.html

Your code does the trick, but it seems a bit redundant to create a copy of the request body as it's already a JS array. You can also use ES6-style for-of loops to make the code a bit more readable:

function (req, res) {
  for (var data of req.body) {
    foxxColl.save(data);
  }
}

@pluma : thanks a lot for your great comment...

Was this page helpful?
0 / 5 - 0 ratings