Saturday 28 August 2021

ArangoDB: Remove a document

 

‘collection.remove()’ method is used to remove a document from the collection.

 

Signature

collection.remove(selector)

collection.remove(selector, options)

 

‘collection.remove( )’ method removes a document described by the selector from the collection. ‘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’ is a json object that contain following attributes.

 

Attribute

Description

waitForSync

If it is set to true, ArangoDB force the synchronizaiton of this operation to the disk.

 

If the waitForSync parameter is not specified or set to false, then the collection’s default waitForSync behavior is applied.

overwrite

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

returnOld

If this flag is set to true, then the previous revision of the document is returned in the output.

silent

If this flag is set to true, no output is returned.

 

Let’s experiment with an example.

127.0.0.1:8529@abc_org> db.user.toArray()
[ 
  { 
    "_key" : "17089", 
    "_id" : "user/17089", 
    "_rev" : "_cRe1GKq---", 
    "id" : 1, 
    "firstName" : "Sailu", 
    "lastName" : "Ptr", 
    "age" : 32 
  }, 
  { 
    "_key" : "17091", 
    "_id" : "user/17091", 
    "_rev" : "_cRe1GKu---", 
    "id" : 2, 
    "firstName" : "Gopi", 
    "lastName" : "Battu", 
    "age" : 33 
  }, 
  { 
    "_key" : "17093", 
    "_id" : "user/17093", 
    "_rev" : "_cRe1GKy---", 
    "id" : 3, 
    "firstName" : "Krishna", 
    "lastName" : "Gurram", 
    "age" : 32 
  }, 
  { 
    "_key" : "17095", 
    "_id" : "user/17095", 
    "_rev" : "_cRe1H-6---", 
    "id" : 4, 
    "firstName" : "Venkat", 
    "lastName" : "Ptr", 
    "age" : 35 
  }, 
  { 
    "_key" : "23404", 
    "_id" : "user/23404", 
    "_rev" : "_cRhMFQ6---", 
    "id" : 5, 
    "firstName" : "Gireesh", 
    "lastName" : "Amara", 
    "age" : 30 
  }, 
  { 
    "_key" : "user-7", 
    "_id" : "user/user-7", 
    "_rev" : "_cRhodqC---", 
    "id" : 7, 
    "firstName" : "Joel", 
    "lastName" : "Battu", 
    "age" : 35, 
    "hobbies" : [ 
      "cricket" 
    ] 
  }, 
  { 
    "_key" : "23418", 
    "_id" : "user/23418", 
    "_rev" : "_cRjYiwi---", 
    "name" : "Manohar Amara", 
    "hobbies" : [ 
      "football", 
      "trekking" 
    ], 
    "age" : 34 
  } 
]

 

Example 1: Remove the document with _key 17089.

127.0.0.1:8529@abc_org> db.user.remove("17089")
{ 
  "_id" : "user/17089", 
  "_key" : "17089", 
  "_rev" : "_cRe1GKq---" 
}

 

Example 2: Remove the document with _id user/17093.

127.0.0.1:8529@abc_org> db.user.remove({"_id": "user/17093"})
{ 
  "_id" : "user/17093", 
  "_key" : "17093", 
  "_rev" : "_cRe1GKy---" 
}

Example 3: Remove the document with _key 23418 and print the removed document.

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


 

Previous                                                    Next                                                    Home

No comments:

Post a Comment