ArangoDB

First Steps with ArangoDB



Starting the ArangoDB server

Create an empty directory, which will hold the database:

> mkdir /tmp/vocbase

First start the ArgangoDB server. You can start it as daemon and redirect the output to a file as follows:

> ./arangod --daemon --pid-file /tmp/arangod.pid --log.file /tmp/arangod.log /tmp/vocbase

This will start the ArangoDB server process, store its process identifier in the file /tmp/arangod.pid and write the output to /tmp/arangod.log. The database files will live in /tmp/vocbase.

First Steps using arangosh

Connecting to the server

Start the ArangoDB JavaScript shell.

> ./arangosh
                                       _     
  __ _ _ __ __ _ _ __   __ _  ___  ___| |__  
 / _` | '__/ _` | '_ \ / _` |/ _ \/ __| '_ \ 
| (_| | | | (_| | | | | (_| | (_) \__ \ | | |
 \__,_|_|  \__,_|_| |_|\__, |\___/|___/_| |_|
                       |___/                 

Welcome to arangosh 1.x.y. Copyright (c) 2012 triAGENS GmbH.
Using Google V8 3.9.4 JavaScript engine.
Using READLINE 6.1.

Connected to Arango DB 127.0.0.1:8529 Version 1.x.y

------------------------------------- Help -------------------------------------
Predefined objects:                                                 
  arango:                                ArangoConnection           
  db:                                    ArangoDatabase             
Example:                                                            
 > db._collections();                    list all collections       
 > db.<coll_name>.all();                 list all documents         
 > id = db.<coll_name>.save({ ... });    save a document            
 > db.<coll_name>.remove(<_id>);         delete a document          
 > db.<coll_name>.document(<_id>);       get a document             
 > help                                  show help pages            
 > helpQueries                           query help                 
 > exit                                                             
arangosh>

This gives you a prompt, where you can issue JavaScript commands.

You might need to specify the endpoint, username and password in order to run the shell on your system. You can use the options --server.endpoint, --server.username and --server.password for this. If you do not specify a password, arangosh will prompt for one.

> ./arangosh --server.endpoint tcp://127.0.0.1:8529 --server.username root

Querying

All documents are stored in collections. All collections are stored in a database.

arangosh> db;
[object ArangoDatabase]

Creating a collection is simple. You can use the _create method of the db variable.

arangosh> db._create("example");
[ArangoCollection 70628, "example" (status loaded)]

After the collection has been create you can easily access it using the path db.example. The collection currently shows as loaded, meaning that its loaded into memory. If you restart the server and access the collection again, it will now show as unloaded. You can also manually unload a collection

arangosh> db.example.unload();
arangosh> db.example;
[ArangoCollection 70628, "example" (status unloaded)]

In order to create new documents in a collection, use the save operator. If the collection is currently unloaded, it will automatically be loaded into memory.

arangosh> db.example.save({ Hallo : "World" });
{ error : false, _id : "70628/1512420", _rev : 1512420 }
arangosh> db.example.save({ name : "Musterman", age : 29 });
{ error : false, _id : "70628/1774564", _rev : 1774564 }

In order to select all elements of a collection, one can use the all operator.

arangosh> start_pretty_print();
use pretty printing
arangosh> db.example.all().toArray();
[
  { 
    _id : "70628/1774564", 
    _rev : 1774564, 
    age : 29, 
    name : "Musterman"
   }, 
  { 
    _id : "70628/1512420", 
    _rev : 1512420, 
    Hallo : "World"
   }
]

This will select and print all documents.