Friday, 27 August 2021

ArangoDB: update: Update the content of a document

‘collection.update’ method updates the content of a document that matches to given selector.

 

Signature

collection.update(selector, data)

collection.update(selector, data, options)

 

‘selector’ is a json object that contain either _id or _key attribute to uniquely identify the document within the collection.

 

Can I provide _rev attribute in the selector object?

Yes, you can provide _rev attribute. When selector contains _rev attribute, replace method first checks that the specified revision is the current revision of that document or not. If not, error is thrown.

 

‘options’ argument is a json object. Following table summarizes the options that are passed to this argument.

 

Option

Description

waitForSync

If it is set to true, Arango force synchronization of document information to disk.

 

If it is not set (or) set to false, Collection’s default waitForSync behavior is applied.

 

overwrite

If this is set to true, _rev attribute in the selector object is ignored.

returnNew

If this is set to true, newly created document is returned in the output.

returnOld

If this is set to true, pld document is returned in the output.

silent

If this flag is set to true, the nothing is returned in the output.

keepNull

By default null values are stored in database. But if you set this attribute to false, then the data with null values will be removed from the target document.

mergeObjects

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

 

If it is set to false, the value in the patch document will overwrite the existing document’s value.

 

Let’s experiment it with an example.

127.0.0.1:8529@abc_org> db.user.document("23418")
{ 
  "_key" : "23418", 
  "_id" : "user/23418", 
  "_rev" : "_cRiGmZa---", 
  "name" : "Manohar Amara", 
  "hobbies" : [ 
    "Cricket" 
  ] 
}

 

Let’s update the above document.

127.0.0.1:8529@abc_org> db.user.update({"_key" : "23418"}, {"hobbies" : ["football", "trekking"], "age": 34}, {"returnNew" : true, "returnOld": true})
{ 
  "_id" : "user/23418", 
  "_key" : "23418", 
  "_rev" : "_cRjYiwi---", 
  "_oldRev" : "_cRiGmZa---", 
  "old" : { 
    "_key" : "23418", 
    "_id" : "user/23418", 
    "_rev" : "_cRiGmZa---", 
    "name" : "Manohar Amara", 
    "hobbies" : [ 
      "Cricket" 
    ] 
  }, 
  "new" : { 
    "_key" : "23418", 
    "_id" : "user/23418", 
    "_rev" : "_cRjYiwi---", 
    "name" : "Manohar Amara", 
    "hobbies" : [ 
      "football", 
      "trekking" 
    ], 
    "age" : 34 
  } 
}

 



 

Previous                                                    Next                                                    Home

No comments:

Post a Comment