This is an introduction to ArangoDB's interface for collections and how handle collections from the JavaScript shell arangosh
. For other languages see the corresponding language API.
The most import call is the call to create a new collection, see _create.
Collection: A collection consists of documents. It is uniquely identified by it's collection identifier. It also has a unique name.
Collection Identifier: A collection identifier identifies a collection in a database. It is an integer and is unique within the database.
Collection Name: A collection name identifies a collection in a database. It is an string and is unique within the database. Unlike the collection identifier it is supplied by the creator of the collection. The collection name can consist of letters, digits and the characters _
(underscore) and -
(dash). However, the first character must be a letter.
All collections in ArangoDB have an unique identifier. This collection identifier identifies a collection and is managed by ArangoDB. Each collection, in addition, has a unique name. This name is managed by the user. The interface allows you to access the collections as:
db._collection(collection-identifier)
or
db._collection(collection-name)
A collection is created by a db._create
call, see _create.
For example: Assume that the collection identifier is 7254820
and the name is demo
, then the collection can be accessed as:
db._collection("demo")
or
db._collection(7254820)
If no collection with such a name or identifier exists, then null
is returned.
There is a short-cut
db.collection-name
This call will either return the collection named collection-name or create a new one with that name and a set of default properties.
collection.drop()
Examples
Drops a collection:
arango> col = db.examples; [ArangoCollection 109757, "examples" (status unloaded)] arango> col.drop() arango> col; [ArangoCollection 109757, "examples" (status deleted)]
collection.truncate()
Examples
Truncates a collection:
arango> col = db.examples; [ArangoCollection 91022, "examples" (status new born)] arango> col.save({ "Hallo" : "World" }); { "_id" : "91022/1532814", "_rev" : 1532814 } arango> col.count(); 1 arango> col.truncate(); arango> col.count(); 0
collection.properties()
waitForSync
: If true
creating a document will only return after the data was synced to disk.journalSize
: The size of the journal in bytes.collection.properties(properties)
waitForSync
: If true
creating a document will only return after the data was synced to disk.journalSize
: The size of the journal in bytes.Note that it is not possible to change the journal size after the journal or datafile has been created. Changing this parameter will only effect newly created journals. Also note that you cannot lower the journal size to less then size of the largest document already stored in the collection.
Examples
Read all properties
arango> db.examples.properties() { "waitForSync" : false, "journalSize" : 33554432 }
Change a property
arango> db.examples.properties({ waitForSync : false }) { "waitForSync" : false, "journalSize" : 33554432 }
collection.figures()
alive.count
: The number of living documents.alive.size
: The total size in bytes used by all living documents.dead.count
: The number of dead documents.dead.size
: The total size in bytes used by all dead documents.dead.deletion
: The total number of deletion markers.datafiles.count
: The number of active datafiles.datafiles.fileSize
: The total filesize of the active datafiles.journals.count
: The number of journal files.journals.fileSize
: The total filesize of the journal files.Examples
arango> db.demo.figures() { "numberDatafiles" : 1, "numberAlive" : 1, "numberDead" : 1, "sizeAlive" : 24, "sizeDead" : 24, "numberDeletion" : 1 }
collection.load()
Examples
arango> col = db.example; [ArangoCollection 164208316, "example" (status unloading)] arango> col.load(); arango> col; [ArangoCollection 164208316, "example" (status loaded)]
collection.unload()
Examples
arango> col = db.example; [ArangoCollection 164208316, "example" (status loaded)] arango> col.unload(); arango> col; [ArangoCollection 164208316, "example" (status unloaded)]
collection.rename(new-name)
Examples
arango> c = db.example; [ArangoCollection 68519, "example" (status new born)] arango> c.rename("better-example"); arango> c; [ArangoCollection 68519, "better-example" (status new born)]
db._collection(collection-identifier)
db._collection(collection-name)
Examples
Get a collection by name:
arango> db._collection("demo"); [ArangoCollection 145387, "demo" (status loaded)]
Get a collection by id:
arango> db._collection(145387); [ArangoCollection 145387, "demo" (status loaded)]
Unknown collection:
arango> db._collection("unknown") null
db._create(collection-name)
waitForSync
is false
.
db._create(collection-name, properties)
waitForSync
(optional, default false
): If true
creating a document will only return after the data was synced to disk.journalSize
(optional, default is a configuration parameter): The maximal size of a journal or datafile. Note that this also limits the maximal size of a single object. Must be at least 1MB.Examples
With defaults:
arango> c = db._create("cars"); [ArangoCollection 111137, "cars" (status loaded)] arango> c.properties() { "waitForSync" : false, "journalSize" : 33554432 }
With properties:
arango> c = db._create("cars", { waitForSync : true, journalSize : 1024 * 1204 }); [ArangoCollection 96384, "cars" (status loaded)] arango> c.properties() { "waitForSync" : true, "journalSize" : 1232896 }
db._collections()
Examples
arango> db.examples.load(); arango> var d = db.demo; arango> db._collections(); [[ArangoCollection 96393, "examples" (status loaded)], [ArangoCollection 1407113, "demo" (status new born)]]
db.collection-name
Examples
arango> db.examples; [ArangoCollection 110371, "examples" (status new born)]
db._drop(collection)
db._drop(collection-identifier)
db._drop(collection-name)
Examples
Drops a collection:
arango> col = db.examples; [ArangoCollection 109757, "examples" (status unloaded)] arango> db._drop(col) arango> col; [ArangoCollection 109757, "examples" (status deleted)]
Drops a collection identified by name:
arango> col = db.examples; [ArangoCollection 85198, "examples" (status new born)] arango> db._drop("examples"); arango> col; [ArangoCollection 85198, "examples" (status deleted)]
db._truncate(collection)
db._truncate(collection-identifier)
db._truncate(collection-name)
Examples
Truncates a collection:
arango> col = db.examples; [ArangoCollection 91022, "examples" (status new born)] arango> col.save({ "Hallo" : "World" }); { "_id" : "91022/1532814", "_rev" : 1532814 } arango> col.count(); 1 arango> db._truncate(col); arango> col.count(); 0
Truncates a collection identified by name:
arango> col = db.examples; [ArangoCollection 91022, "examples" (status new born)] arango> col.save({ "Hallo" : "World" }); { "_id" : "91022/1532814", "_rev" : 1532814 } arango> col.count(); 1 arango> db._truncate("examples"); arango> col.count(); 0