Thursday 19 August 2021

ArangoDB: Find the document by id or key

Signature

collection.document(document-handle)

collection.document(document-key)

 

‘collection.document’ method finds an element either by id (_id) or by document key (_key).

 

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

 

Example: Find the document by id

127.0.0.1:8529@abc_org> db.user.document("user/17095")
{ 
  "_key" : "17095", 
  "_id" : "user/17095", 
  "_rev" : "_cRe1H-6---", 
  "id" : 4, 
  "firstName" : "Venkat", 
  "lastName" : "Ptr", 
  "age" : 35 
}

 

Example: Find the document by key

127.0.0.1:8529@abc_org> db.user.document("17095")
{ 
  "_key" : "17095", 
  "_id" : "user/17095", 
  "_rev" : "_cRe1H-6---", 
  "id" : 4, 
  "firstName" : "Venkat", 
  "lastName" : "Ptr", 
  "age" : 35 
}

This method throws 'document not found' error when the document is not exist with given id or key.

127.0.0.1:8529@abc_org> db.user.document("17098")
JavaScript exception in file '/usr/local/Cellar/arangodb/3.7.11_1/share/arangodb3/js/client/modules/@arangodb/arangosh.js' at 99,7: ArangoError 1202: document not found
!      throw error;
!      ^
stacktrace: ArangoError: document not found
    at Object.exports.checkRequestResult (/usr/local/Cellar/arangodb/3.7.11_1/share/arangodb3/js/client/modules/@arangodb/arangosh.js:97:21)
    at ArangoCollection.document (/usr/local/Cellar/arangodb/3.7.11_1/share/arangodb3/js/client/modules/@arangodb/arango-collection.js:764:12)
    at <shell command>:1:9

You can even pass a json object as an argument to document method.

db.user.document({"_key" : "17095"})

127.0.0.1:8529@abc_org> db.user.document({"_key" : "17095"})
{ 
  "_key" : "17095", 
  "_id" : "user/17095", 
  "_rev" : "_cRe1H-6---", 
  "id" : 4, 
  "firstName" : "Venkat", 
  "lastName" : "Ptr", 
  "age" : 35 
}

You can even specify _rev attribute while getting the document.

127.0.0.1:8529@abc_org> db.user.document({"_key" : "17095", "_rev" : "_cRe1H-6---"})
{ 
  "_key" : "17095", 
  "_id" : "user/17095", 
  "_rev" : "_cRe1H-6---", 
  "id" : 4, 
  "firstName" : "Venkat", 
  "lastName" : "Ptr", 
  "age" : 35 
}

Suppose if the document with given revision is not exist, then ArangoDB throws a conflict.

127.0.0.1:8529@abc_org> db.user.document({"_key" : "17095", "_rev" : "_cRe1H-6"})
JavaScript exception in file '/usr/local/Cellar/arangodb/3.7.11_1/share/arangodb3/js/client/modules/@arangodb/arangosh.js' at 99,7: ArangoError 1200: conflict
!      throw error;
!      ^
stacktrace: ArangoError: conflict
    at Object.exports.checkRequestResult (/usr/local/Cellar/arangodb/3.7.11_1/share/arangodb3/js/client/modules/@arangodb/arangosh.js:97:21)
    at ArangoCollection.document (/usr/local/Cellar/arangodb/3.7.11_1/share/arangodb3/js/client/modules/@arangodb/arango-collection.js:764:12)
    at <shell command>:1:9

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment