Skip to content

Latest commit

 

History

History
125 lines (90 loc) · 2.58 KB

basic_operations.md

File metadata and controls

125 lines (90 loc) · 2.58 KB

Basic database operations

create database

  // create database 
  arangoDB.createDatabase("myDatabase");
  

drop database

  // drop database 
  arangoDB.db("myDatabase").drop();
  

Basic collection operations

create collection

  // create collection
  arangoDB.db("myDatabase").createCollection("myCollection", null);
  

delete collection by name

  // delete collection 
  arangoDB.db("myDatabase").collection("myCollection").drop();
  

delete all documents in the collection

  arangoDB.db("myDatabase").collection("myCollection").truncate();

Basic document operations

Every document operations works with POJOs (e.g. MyObject), VelocyPack (VPackSlice) and Json (String).

For the next examples we use a small object:

  public class MyObject {

    private String key;
    private String name;
    private int age;

    public MyObject(String name, int age) {
      this();
      this.name = name;
      this.age = age;
    }

    public MyObject() {
      super();
    }

    /*
     *  + getter and setter
     */
  
  }  

insert document

  MyObject myObject = new MyObject("Homer", 38);
  arangoDB.db("myDatabase").collection("myCollection").insertDocument(myObject);
    

When creating a document, the attributes of the object will be stored as key-value pair E.g. in the previous example the object was stored as follows:

  "name" : "Homer"
  "age" : "38"

delete document

  arangoDB.db("myDatabase").collection("myCollection").deleteDocument(myObject.getKey);
  

update document

  arangoDB.db("myDatabase").collection("myCollection").updateDocument(myObject.getKey, myUpdatedObject);
  

replace document

  arangoDB.db("myDatabase").collection("myCollection").replaceDocument(myObject.getKey, myObject2);
  

read document by key (as JavaBean)

  MyObject document = arangoDB.db("myDatabase").collection("myCollection").getDocument(myObject.getKey, MyObject.class);
  document.getName();
  document.getAge();
  

read document (as VelocyPack)

  VPackSlice document = arangoDB.db("myDatabase").collection("myCollection").getDocument(myObject.getKey, VPackSlice.class);
  document.get("name").getAsString();
  document.get("age").getAsInt();
  

read document (as Json)

  arangoDB.db("myDatabase").collection("myCollection").getDocument(myObject.getKey, String.class);
  

read document by id

  arangoDB.db("myDatabase").getDocument("myCollection/myKey", MyObject.class);