0

according to ArangoDB documentation, INSERT operation is applicable for a single document insertion. I have an array which may have thousands of objects (documents). Is there a way to INSERT this array of document INTO a collection with a single query?

DeSpider
  • 303
  • 2
  • 16

3 Answers3

2

I believe you've misunderstood the documentation, which says:

Only a single INSERT statement per collection is allowed per AQL query

You can have multiple INSERT statements per AQL query (subject to the above limitation, amongst others), and each of them can entail multiple insertions.

Here's an example of 1000 insertions executed successfully as one AQL query:

FOR n in 1..1000
 INSERT {_from: "tasks/1", _to: CONCAT("tasks/", TO_STRING(n))} in depends
 COLLECT WITH COUNT INTO c
 RETURN c
peak
  • 72,551
  • 14
  • 101
  • 128
1

Another approach would be to access the database with arangosh:

You can use the method collection.insert(array) to insert an array consisting of multiple documents into a collection.

Example: Insert two documents into the collection "example"

db.example.insert([{ id : "doc1" }, { id : "doc2" }]);
[ 
  { 
    "_id" : "example/12156601", 
    "_key" : "12156601", 
    "_rev" : "_WEnsap6---" 
  }, 
  { 
    "_id" : "example/12156605", 
    "_key" : "12156605", 
    "_rev" : "_WEnsap6--_" 
  } 
]

The method is documented at: https://docs.arangodb.com/3.2/Manual/DataModeling/Documents/DocumentMethods.html

0

BULK Insert in AQL v3.6 is as following:

db.collection('collection_name').save([{ id : "doc1" }, { id : "doc2" }]);

as described in docs and I have tested it in my project using NodeJS. https://www.arangodb.com/docs/stable/programs-arangosh-details.html#database-wrappers

RS Roshi
  • 3
  • 2