Saturday, 21 August 2021

ArangoDB: Executing queries with arangosh

You can execute queries against a database fro arango shell using db object.

 

For example, let’s see how to get list of collections from a database.

 

Step 1: Connect to example database by executing below command.

arangosh --server.username "krishna" --server.database example

$arangosh --server.username "krishna" --server.database example
Please specify a password: 

                                       _     
  __ _ _ __ __ _ _ __   __ _  ___  ___| |__  
 / _` | '__/ _` | '_ \ / _` |/ _ \/ __| '_ \ 
| (_| | | | (_| | | | | (_| | (_) \__ \ | | |
 \__,_|_|  \__,_|_| |_|\__, |\___/|___/_| |_|
                       |___/                 

arangosh (ArangoDB 3.7.11 [darwin] 64bit, using build , VPack 0.1.33, RocksDB 6.8.0, ICU 64.2, V8 7.9.317, OpenSSL 1.1.1k  25 Mar 2021)
Copyright (c) ArangoDB GmbH

Command-line history will be persisted when the shell is exited. You can use `--console.history false` to turn this off
Connected to ArangoDB 'http+tcp://127.0.0.1:8529, version: 3.7.11 [SINGLE, server], database: 'example', username: 'krishna'

Type 'tutorial' for a tutorial or 'help' to see common examples

 

Step 2: Execute the command db._collecitons() to get all the collections available in the database example.

127.0.0.1:8529@example> db._collections()
[ 
  [ArangoCollection 1495, "_analyzers" (type document, status loaded)], 
  [ArangoCollection 1510, "_appbundles" (type document, status loaded)], 
  [ArangoCollection 1507, "_apps" (type document, status loaded)], 
  [ArangoCollection 1498, "_aqlfunctions" (type document, status loaded)], 
  [ArangoCollection 1519, "_fishbowl" (type document, status loaded)], 
  [ArangoCollection 1513, "_frontend" (type document, status loaded)], 
  [ArangoCollection 1492, "_graphs" (type document, status loaded)], 
  [ArangoCollection 1504, "_jobs" (type document, status loaded)], 
  [ArangoCollection 1516, "_modules" (type document, status loaded)], 
  [ArangoCollection 1501, "_queues" (type document, status loaded)], 
  [ArangoCollection 2751, "user" (type document, status loaded)] 
]

You can even pass a json object or array.

db.user.document([{"_key" : "17091"}])

127.0.0.1:8529@abc_org> db.user.document([{"_key" : "17091"}])
[ 
  { 
    "_key" : "17091", 
    "_id" : "user/17091", 
    "_rev" : "_cRe1GKu---", 
    "id" : 2, 
    "firstName" : "Gopi", 
    "lastName" : "Battu", 
    "age" : 33 
  } 
]


You can use all the three combinations like _key, _id and json object in the array argument.

127.0.0.1:8529@abc_org> db.user.document(["user/17095", "17089", {"_key" : "17091"}])
[ 
  { 
    "_key" : "17095", 
    "_id" : "user/17095", 
    "_rev" : "_cRe1H-6---", 
    "id" : 4, 
    "firstName" : "Venkat", 
    "lastName" : "Ptr", 
    "age" : 35 
  }, 
  { 
    "_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 
  } 
]


 

Previous                                                    Next                                                    Home

No comments:

Post a Comment