Wednesday, 25 August 2021

ArangoDB: Replace the existing document

‘collection.replace’ method replace the existing document specified by the selector.

 

Signature

collection.replace(selector, data)

collection.replace(selector, data, options)

 

‘selector’ is a json object which must contain _id or _key attribute.

 

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.

 

Example

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

Let’s replace the content of the document with key 23418.

 

db.user.replace({"_key" : "23418"}, {"name" : "Manohar Amara", "hobbies": ["Cricket"] }, {"returnNew": true, "returnOld" : true})

127.0.0.1:8529@abc_org> db.user.replace({"_key" : "23418"}, {"name" : "Manohar Amara", "hobbies": ["Cricket"] }, {"returnNew": true, "returnOld" : true})
{ 
  "_id" : "user/23418", 
  "_key" : "23418", 
  "_rev" : "_cRiGmZa---", 
  "_oldRev" : "_cRhMbNm---", 
  "old" : { 
    "_key" : "23418", 
    "_id" : "user/23418", 
    "_rev" : "_cRhMbNm---", 
    "id" : 6, 
    "firstName" : "Manohar", 
    "lastName" : "Amara", 
    "age" : 30 
  }, 
  "new" : { 
    "_key" : "23418", 
    "_id" : "user/23418", 
    "_rev" : "_cRiGmZa---", 
    "name" : "Manohar Amara", 
    "hobbies" : [ 
      "Cricket" 
    ] 
  } 
}

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



 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment