Tuesday 27 July 2021

ArangoDB: Working with documents

Document store the data in the form of JSON objects. Every document has a key and handle.

 

Document key

Document key is a string used to uniquely identify a document within the collection. Document key is stored in _key attribute of the document. Once _key attribute is assigned with a value, it is immutable, its value is not going to be changed any time.

 

Is the document key values are indexed?

Yes

 

Can I provide custom value to _key attribute?

Yes, you can. But you need to make sure that the _key value is unique in the collection and it is immutable.

 

Document Handle

Document handle is used to uniquely identify a document within the database.  It is a string consist of collection name followed by / followed by the value of _key attribute.

 

Example

127.0.0.1:8529@abc_org> userCollection.save({"id" : 1, "firstName": "Ram"})
{ 
  "_id" : "user/12977", 
  "_key" : "12977", 
  "_rev" : "_cRdSijO---" 
}

In the above example, 12977 is the Document key and "user/12977" is the document handle.

 

Document Revision

Every document has a revision specified using _rev attribute. _rev attribute is read-only to the user. It is calculated by ArangoDB server. Whenever a document is updated, this revision value is going to be changed.

 

For example, lets create a document.

127.0.0.1:8529@abc_org> userCollection.save({"lastName" : "Gurram", "firstName": "Ram"})
{ 
  "_id" : "user/14434", 
  "_key" : "14434", 
  "_rev" : "_cRd20V6---" 
}


Let’s update the document and confirm that the revision is updated.

127.0.0.1:8529@abc_org> userCollection.update("user/14434", {"lastName": "Battu"}, false, false)
{ 
  "_id" : "user/14434", 
  "_key" : "14434", 
  "_rev" : "_cRd4ppe---", 
  "_oldRev" : "_cRd20V6---" 
}


You can observe that new revision is updated. After some time old revision content is deleted.

 

Can I perform operations on multiple documents using single command?

Yes, from ArangoDB 3.0 onwards, you can perform actions on multiple documents in a single command.

 

      ArangoDB: all(): Get all the documents in a collection
      ArangoDB: Convert the cursor to array
      ArangoDB: Traverse a cursor using next and hasNext methods
      ArangoDB: limit(): limit number of documents to be returned
      ArangoDB: skip(): skip number of documents
      ArangoDB: Query by example
      ArangoDB: firstExample: Get one document that matches to given example
      ArangoDB: range, closedRange: Get all the documents where attribute is in given range
      ArangoDB: any: Get any one document
      ArangoDB: count: Count number of live documents
      ArangoDB: toArray: Convert the collection into an array
      ArangoDB: Find the document by id or key
      ArangoDB: Get multiple documents in one call
      ArangoDB: Executing queries with arangosh
      ArangoDB: exists: Check whether document exists or not
      ArangoDB: Check for the existence of multiple documents
      ArangoDB: Get documents by keys
      ArangoDB: insert/save: Insert new documents to the collection
      ArangoDB: Replace the existing document
      ArangoDB: update: Update the content of a document
      ArangoDB: Remove a document
      ArangoDB: Remove documents by keys
      ArangoDB: Remove by example
      ArangoDB: Replace by Example
      ArangoDB: Update the documents by example
      ArangoDB: Get the type of collection
      ArangoDB: Get document id from key
      ArangoDB: Working with edge documents
      ArangoDB: Enforce schema at collection level

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment