Tuesday 31 August 2021

ArangoDB: Replace by Example

‘replaceByExample’ method is used to replace the entire document body of matching example with new value.

 

Signature

collection.replaceByExample(example, newValue)

collection.replaceByExample(document, newValue, waitForSync)

collection.replaceByExample(document, newValue, waitForSync, limit)

 

if ‘waitForSync’ parameter is set to true, then it forces the synchronization of replaceByExample operation to disk. . If the waitForSync parameter is not specified or set to false, then the collection’s default waitForSync behavior is applied.

 

‘limit’ parameter is used to restrict the number of documents to be affected with this operation.

 

Let’s experiment with an example.

127.0.0.1:8529@abc_org> db.test.toArray()
[ 
  { 
    "_key" : "33825", 
    "_id" : "test/33825", 
    "_rev" : "_cS-_Uj----", 
    "a" : 1, 
    "b" : 1, 
    "c" : 1 
  }, 
  { 
    "_key" : "33827", 
    "_id" : "test/33827", 
    "_rev" : "_cS-_YP----", 
    "a" : 1, 
    "b" : 0, 
    "c" : 1 
  }, 
  { 
    "_key" : "33835", 
    "_id" : "test/33835", 
    "_rev" : "_cS-_bQ2---", 
    "a" : 1, 
    "b" : 0, 
    "c" : 0 
  }, 
  { 
    "_key" : "33837", 
    "_id" : "test/33837", 
    "_rev" : "_cS-_g66---", 
    "a" : 0, 
    "b" : 0, 
    "c" : 0 
  }, 
  { 
    "_key" : "33845", 
    "_id" : "test/33845", 
    "_rev" : "_cS-_lkC---", 
    "a" : 0, 
    "b" : 0, 
    "c" : 1 
  }, 
  { 
    "_key" : "33847", 
    "_id" : "test/33847", 
    "_rev" : "_cS-_qWG---", 
    "a" : 0, 
    "b" : 1, 
    "c" : 0 
  }, 
  { 
    "_key" : "33849", 
    "_id" : "test/33849", 
    "_rev" : "_cS-_tHy---", 
    "a" : 0, 
    "b" : 1, 
    "c" : 1 
  } 
]

 

If a document has {"a" : 0}, then I want to replace it with {"a" : 1, "x" : 1, "y" : 1}.

db.test.replaceByExample({"a" : 0}, {"a" : 1, "x" : 1, "y" : 1})

127.0.0.1:8529@abc_org> db.test.replaceByExample({"a" : 0}, {"a" : 1, "x" : 1, "y" : 1})
4

127.0.0.1:8529@abc_org> db.test.toArray()
[ 
  { 
    "_key" : "33825", 
    "_id" : "test/33825", 
    "_rev" : "_cS-_Uj----", 
    "a" : 1, 
    "b" : 1, 
    "c" : 1 
  }, 
  { 
    "_key" : "33827", 
    "_id" : "test/33827", 
    "_rev" : "_cS-_YP----", 
    "a" : 1, 
    "b" : 0, 
    "c" : 1 
  }, 
  { 
    "_key" : "33835", 
    "_id" : "test/33835", 
    "_rev" : "_cS-_bQ2---", 
    "a" : 1, 
    "b" : 0, 
    "c" : 0 
  }, 
  { 
    "_key" : "33837", 
    "_id" : "test/33837", 
    "_rev" : "_cS-CmaK---", 
    "a" : 1, 
    "x" : 1, 
    "y" : 1 
  }, 
  { 
    "_key" : "33845", 
    "_id" : "test/33845", 
    "_rev" : "_cS-CmaK--A", 
    "a" : 1, 
    "x" : 1, 
    "y" : 1 
  }, 
  { 
    "_key" : "33847", 
    "_id" : "test/33847", 
    "_rev" : "_cS-CmaO---", 
    "a" : 1, 
    "x" : 1, 
    "y" : 1 
  }, 
  { 
    "_key" : "33849", 
    "_id" : "test/33849", 
    "_rev" : "_cS-CmaO--A", 
    "a" : 1, 
    "x" : 1, 
    "y" : 1 
  } 
]

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment