Tuesday, 24 August 2021

ArangoDB: insert/save: Insert new documents to the collection

Signature

collection.insert(data)

collection.save(data)

collection.insert(data, options)

collection.save(data, options)

 

‘insert’ and ‘save’ methods are used to create new documents in the collection. ‘insert’ is just an alias to ‘save’ method.

 

Example

127.0.0.1:8529@abc_org> db.user.save({"id": 5, "firstName" : "Gireesh", "lastName" : "Amara", "age" : 30})
{ 
  "_id" : "user/23404", 
  "_key" : "23404", 
  "_rev" : "_cRhMFQ6---" 
}

127.0.0.1:8529@abc_org> db.user.insert({"id": 6, "firstName" : "Manohar", "lastName" : "Amara", "age" : 30})
{ 
  "_id" : "user/23418", 
  "_key" : "23418", 
  "_rev" : "_cRhMbNm---" 
}

 

As you see above snippet, _id, _key and _rev fields are auto generated by ArangoDB server. _key attribute is used to identify the document uniquely within the collection.

 

Can I specify a value to _key attribute?

Yes, you can explicitly specify a value to _key attribute, but make sure that there is no document in the collection with same _key attriburte value.

127.0.0.1:8529@abc_org> db.user.insert({"_key" : "user-7", "id": 7, "firstName" : "Rambabu", "lastName" : "Battu", "age" : 35})
{ 
  "_id" : "user/user-7", 
  "_key" : "user-7", 
  "_rev" : "_cRhOw-O---" 
}

 

Can I add _rev and _id attributes explicitly?

No, these are generated by ArangoDB and added to the document. _id is immutable, this value will never change during the lifetime of the document. _rev specifies the revision of the document, this value will change whenever there is update on the document.

 

options argument

options argument is a json object, you can pass following options to the options argument.

 

Option

Description

waitForSync

If this is set true, the ArangoDB forces the synchronization of the document creation operation to disk. If this is set to true, it overrides the collection default behavior.

 

If this option is not set or set to false, then collections default waitForSync behavior is applied

silent

If this option is set to true, then this insert/save method do not return any output.

returnNew

If this is set to true, complete new document is returned in the output under the attribute new.

returnOld

If this is set to true, the complete old document is returned in the output under the attribute old.

 

This option is only available in combination with the overwrite option.

overwrite

If this is set to true, then existing document will be replaced.

overwriteMode

Following values are supported.

 

ignore: if a document with the specified _key value exists already, nothing will be done.

 

replace: if a document with the specified _key value exists already, it will be overwritten with the specified document value.

 

This mode will also be used when no overwrite mode is specified but the overwrite flag is set to true.

 

update: if a document with the specified _key value exists already, it will be patched with the specified document value.

 

conflict: if a document with the specified _key value exists already, then operation is failed with unique constraint violation error. This is the default overwrite mode.

 

keepNull

If it is set to false, all attributes in data with null values will be removed from the document.

mergeObjects

If it is set to false, content of the patch document will override the existing document value.

 

If it is set to true, objects will be merged.

 

Let’s override the content of already existing document using insert/save method.

127.0.0.1:8529@abc_org> db.user.document("user-7")
{ 
  "_key" : "user-7", 
  "_id" : "user/user-7", 
  "_rev" : "_cRhOw-O---", 
  "id" : 7, 
  "firstName" : "Rambabu", 
  "lastName" : "Battu", 
  "age" : 35 
}

Execute following statement to override the document with key "user-7".

db.user.save({"_key" : "user-7", "firstName" : "Joel", "hobbies" : ["cricket"]}, {"overwrite" : true, "overwriteMode": "update"})


127.0.0.1:8529@abc_org> db.user.save({"_key" : "user-7", "firstName" : "Joel", "hobbies" : ["cricket"]}, {"overwrite" : true, "overwriteMode": "update"})
{ 
  "_id" : "user/user-7", 
  "_key" : "user-7", 
  "_rev" : "_cRhodqC---", 
  "_oldRev" : "_cRhOw-O---" 
}

127.0.0.1:8529@abc_org> db.user.document("user-7")
{ 
  "_key" : "user-7", 
  "_id" : "user/user-7", 
  "_rev" : "_cRhodqC---", 
  "id" : 7, 
  "firstName" : "Joel", 
  "lastName" : "Battu", 
  "age" : 35, 
  "hobbies" : [ 
    "cricket" 
  ] 
}



  

Previous                                                    Next                                                    Home

No comments:

Post a Comment