Tuesday 31 August 2021

ArangoDB: Update the documents by example

‘collection.updateByExample()’ method is used to update all the matched documents with new content. It will not replace entire document, it just updated the matched values in the document.

 

Signature

collection.updateByExample(example, newValue)

collection.updateByExample(document, newValue, keepNull, waitForSync, limit)

collection.updateByExample(document, newValue, options)

 

Attributes with null values are stored in the database by default. If 'keepNull' is set to false, then all the attributes in data with null values will be removed from the target document.

 

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 behaviour is applied.

 

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

 

Let’s experiment it 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-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 
  } 
]

 

If a document contain {"x" : 1}, then update it with {"x" : 0, "b": 1}

 

127.0.0.1:8529@abc_org> db.test.updateByExample({"x" : 1}, {"x" : 0, "b": 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-WEUm---", 
    "a" : 1, 
    "x" : 0, 
    "y" : 1, 
    "b" : 1 
  }, 
  { 
    "_key" : "33845", 
    "_id" : "test/33845", 
    "_rev" : "_cS-WEUm--A", 
    "a" : 1, 
    "x" : 0, 
    "y" : 1, 
    "b" : 1 
  }, 
  { 
    "_key" : "33847", 
    "_id" : "test/33847", 
    "_rev" : "_cS-WEUm--C", 
    "a" : 1, 
    "x" : 0, 
    "y" : 1, 
    "b" : 1 
  }, 
  { 
    "_key" : "33849", 
    "_id" : "test/33849", 
    "_rev" : "_cS-WEUm--E", 
    "a" : 1, 
    "x" : 0, 
    "y" : 1, 
    "b" : 1 
  } 
]

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment