Thursday, 2 September 2021

ArangoDB: AQL: Insert multiple documents

  Below snippet is used to insert multiple documents to ArangoDB person collection.

dataToInsert = [
  {"id" : 124, "firstName" : "Janani"},
  {"id": 125, "firstName" : "Prathap", "age" : 35},
  {"id": 126, "firstName": "Saurav", "lastName" : "Sarkar", "age": 41}
]

for(docIndex in dataToInsert){
  queryToExecute = "INSERT " + JSON.stringify(dataToInsert[docIndex]) + " INTO person"
  db._query(queryToExecute)
}

 

Arangosh execution code

127.0.0.1:8529@abc_org> db._create("person")
[ArangoCollection 40320, "person" (type document, status loaded)]

127.0.0.1:8529@abc_org> dataToInsert = [
...>   {"id" : 124, "firstName" : "Janani"},
...>   {"id": 125, "firstName" : "Prathap", "age" : 35},
...>   {"id": 126, "firstName": "Saurav", "lastName" : "Sarkar", "age": 41}
...> ]
[ 
  { 
    "id" : 124, 
    "firstName" : "Janani" 
  }, 
  { 
    "id" : 125, 
    "firstName" : "Prathap", 
    "age" : 35 
  }, 
  { 
    "id" : 126, 
    "firstName" : "Saurav", 
    "lastName" : "Sarkar", 
    "age" : 41 
  } 
]

127.0.0.1:8529@abc_org> for(docIndex in dataToInsert){
...>   queryToExecute = "INSERT " + JSON.stringify(dataToInsert[docIndex]) + " INTO person"
...>   db._query(queryToExecute)
...> }
[object ArangoQueryCursor, count: 0, cached: false, hasMore: false]

 

Query person collection and retrieve all the data

127.0.0.1:8529@abc_org> db.person.toArray()
[ 
  { 
    "_key" : "40338", 
    "_id" : "person/40338", 
    "_rev" : "_cSAdFWq---", 
    "id" : 124, 
    "firstName" : "Janani" 
  }, 
  { 
    "_key" : "40341", 
    "_id" : "person/40341", 
    "_rev" : "_cSAdFWu---", 
    "id" : 125, 
    "firstName" : "Prathap", 
    "age" : 35 
  }, 
  { 
    "_key" : "40344", 
    "_id" : "person/40344", 
    "_rev" : "_cSAdFWy---", 
    "id" : 126, 
    "firstName" : "Saurav", 
    "lastName" : "Sarkar", 
    "age" : 41 
  } 
]

 

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment