ArangoDB v2.8 reached End of Life (EOL) and is no longer supported.
This documentation is outdated. Please see the most recent version here: Try latest
Database Methods
Collection
db._collection(collection-name)
Returns the collection with the given name or null if no such collection
exists.
db._collection(collection-identifier)
Returns the collection with the given identifier or null if no such
collection exists. Accessing collections by identifier is discouraged for
end users. End users should access collections using the collection name.
Examples
Get a collection by name:
arangosh> db._collection("demo");
[ArangoCollection 16468097, "demo" (type document, status loaded)]
Get a collection by id:
arangosh> db._collection(123456);
[ArangoCollection 123456, "demo" (type document, status loaded)]
Unknown collection:
arangosh> db._collection("unknown");
null
Create
db._create(collection-name)
Creates a new document collection named collection-name.
If the collection name already exists or if the name format is invalid, an
error is thrown. For more information on valid collection names please refer
to the naming conventions.
db._create(collection-name, properties)
properties must be an object with the following attributes:
- 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.
- isSystem (optional, default is false): If true, create a
system collection. In this case collection-name should start with
an underscore. End users should normally create non-system collections
only. API implementors may be required to create system collections in
very special occasions, but normally a regular collection will do.
- isVolatile (optional, default is false): If true then the
collection data is kept in-memory only and not made persistent. Unloading
the collection will cause the collection data to be discarded. Stopping
or re-starting the server will also cause full loss of data in the
collection. Setting this option will make the resulting collection be
slightly faster than regular collections because ArangoDB does not
enforce any synchronization to disk and does not calculate any CRC
checksums for datafiles (as there are no datafiles).
- keyOptions (optional): additional options for key generation. If
specified, then keyOptions should be a JSON array containing the
following attributes (note: some of them are optional):
- type: specifies the type of the key generator. The currently available generators are traditional and autoincrement.
- allowUserKeys: if set to true, then it is allowed to supply own key values in the _key attribute of a document. If set to false, then the key generator will solely be responsible for generating keys and supplying own key values in the _key attribute of documents is considered an error.
- increment: increment value for autoincrement key generator. Not used for other key generator types.
- offset: initial offset value for autoincrement key generator.
Not used for other key generator types.
- numberOfShards (optional, default is 1): in a cluster, this value
determines the number of shards to create for the collection. In a single
server setup, this option is meaningless.
- shardKeys (optional, default is [ “_key” ]): in a cluster, this
attribute determines which document attributes are used to determine the
target shard for documents. Documents are sent to shards based on the
values they have in their shard key attributes. The values of all shard
key attributes in a document are hashed, and the hash value is used to
determine the target shard. Note that values of shard key attributes cannot
be changed once set.
This option is meaningless in a single server setup.
When choosing the shard keys, one must be aware of the following rules and limitations: In a sharded collection with more than one shard it is not possible to set up a unique constraint on an attribute that is not the one and only shard key given in shardKeys. This is because enforcing a unique constraint would otherwise make a global index necessary or need extensive communication for every single write operation. Furthermore, if _key is not the one and only shard key, then it is not possible to set the _key attribute when inserting a document, provided the collection has more than one shard. Again, this is because the database has to enforce the unique constraint on the _key attribute and this can only be done efficiently if this is the only shard key by delegating to the individual shards.
db._create(collection-name, properties, type)
Specifies the optional type of the collection, it can either be document or edge. On default it is document. Instead of giving a type you can also use db._createEdgeCollection or db._createDocumentCollection.
Examples
With defaults:
arangosh> c = db._create("users");
[ArangoCollection 1076840577, "users" (type document, status loaded)]
arangosh> c.properties();
{
"doCompact" : true,
"journalSize" : 1048576,
"isSystem" : false,
"isVolatile" : false,
"waitForSync" : false,
"keyOptions" : {
"type" : "traditional",
"allowUserKeys" : true
},
"indexBuckets" : 8
}
arangosh> c = db._create("users");
arangosh> c.properties();
With properties:
arangosh> c = db._create("users", { waitForSync : true, journalSize : 1024 * 1204 });
[ArangoCollection 1077954689, "users" (type document, status loaded)]
arangosh> c.properties();
{
"doCompact" : true,
"journalSize" : 1232896,
"isSystem" : false,
"isVolatile" : false,
"waitForSync" : true,
"keyOptions" : {
"type" : "traditional",
"allowUserKeys" : true
},
"indexBuckets" : 8
}
arangosh> c = db._create("users", { waitForSync : true, journalSize : 1024 * 1204 });
arangosh> c.properties();
With a key generator:
arangosh> db._create("users", { keyOptions: { type: "autoincrement", offset: 10, increment: 5 } });
[ArangoCollection 1077037185, "users" (type document, status loaded)]
arangosh> db.users.save({ name: "user 1" });
{
"_id" : "users/10",
"_rev" : "1077364865",
"_key" : "10"
}
arangosh> db.users.save({ name: "user 2" });
{
"_id" : "users/15",
"_rev" : "1077561473",
"_key" : "15"
}
arangosh> db.users.save({ name: "user 3" });
{
"_id" : "users/20",
"_rev" : "1077758081",
"_key" : "20"
}
arangosh> db._create("users", { keyOptions: { type: "autoincrement", offset: 10, increment: 5 } });
arangosh> db.users.save({ name: "user 1" });
arangosh> db.users.save({ name: "user 2" });
arangosh> db.users.save({ name: "user 3" });
With a special key option:
arangosh> db._create("users", { keyOptions: { allowUserKeys: false } });
[ArangoCollection 1078151297, "users" (type document, status loaded)]
arangosh> db.users.save({ name: "user 1" });
{
"_id" : "users/1078478977",
"_rev" : "1078478977",
"_key" : "1078478977"
}
arangosh> db.users.save({ name: "user 2", _key: "myuser" });
[ArangoError 1222: collection does not allow using user-defined keys]
arangosh> db.users.save({ name: "user 3" });
{
"_id" : "users/1078806657",
"_rev" : "1078806657",
"_key" : "1078806657"
}
arangosh> db._create("users", { keyOptions: { allowUserKeys: false } });
arangosh> db.users.save({ name: "user 1" });
arangosh> db.users.save({ name: "user 2", _key: "myuser" });
arangosh> db.users.save({ name: "user 3" });
db._createEdgeCollection(collection-name)
Creates a new edge collection named collection-name. If the
collection name already exists an error is thrown. The default value
for waitForSync is false.
db._createEdgeCollection(collection-name, properties)
properties must be an object with the following attributes:
- waitForSync (optional, default false): If true creating a document will only return after the data was synced to disk.
- journalSize (optional, default is
“configuration parameter”): The maximal size of
a journal or datafile. Note that this also limits the maximal
size of a single object and must be at least 1MB.
db._createDocumentCollection(collection-name)
Creates a new document collection named collection-name. If the
document name already exists and error is thrown.
All Collections
db._collections()
Returns all collections of the given database.
Examples
arangosh> db._collections();
[
[ArangoCollection 5523585, "_jobs" (type document, status loaded)],
[ArangoCollection 2312321, "_routing" (type document, status loaded)],
[ArangoCollection 4081793, "_statisticsRaw" (type document, status loaded)],
[ArangoCollection 5392513, "_queues" (type document, status loaded)],
[ArangoCollection 13387905, "_system_users_users" (type document, status loaded)],
[ArangoCollection 4475009, "_statistics" (type document, status loaded)],
[ArangoCollection 16468097, "demo" (type document, status loaded)],
[ArangoCollection 17320065, "animals" (type document, status loaded)],
[ArangoCollection 4868225, "_statistics15" (type document, status loaded)],
[ArangoCollection 2181249, "_modules" (type document, status loaded)],
[ArangoCollection 3950721, "_aqlfunctions" (type document, status loaded)],
[ArangoCollection 5261441, "_configuration" (type document, status loaded)],
[ArangoCollection 5654657, "_apps" (type document, status loaded)],
[ArangoCollection 1100499073, "example" (type document, status loaded)],
[ArangoCollection 2050177, "_graphs" (type document, status loaded)],
[ArangoCollection 2443393, "_cluster_kickstarter_plans" (type document, status loaded)],
[ArangoCollection 215169, "_users" (type document, status loaded)],
[ArangoCollection 12667009, "_sessions" (type document, status loaded)]
]
arangosh> db._collections();
Collection Name
db.collection-name
Returns the collection with the given collection-name. If no such
collection exists, create a collection named collection-name with the
default properties.
Examples
arangosh> db.example;
[ArangoCollection 1076643969, "example" (type document, status loaded)]
Drop
db._drop(collection)
Drops a collection and all its indexes.
db._drop(collection-identifier)
Drops a collection identified by collection-identifier and all its
indexes. No error is thrown if there is no such collection.
db._drop(collection-name)
Drops a collection named collection-name and all its indexes. No error
is thrown if there is no such collection.
Examples
Drops a collection:
arangosh> col = db.example;
[ArangoCollection 1079003265, "example" (type document, status loaded)]
arangosh> db._drop(col);
arangosh> col;
[ArangoCollection 1079003265, "example" (type document, status loaded)]
Drops a collection identified by name:
arangosh> col = db.example;
[ArangoCollection 1079199873, "example" (type document, status loaded)]
arangosh> db._drop("example");
arangosh> col;
[ArangoCollection 1079199873, "example" (type document, status deleted)]
Truncate
db._truncate(collection)
Truncates a collection, removing all documents but keeping all its
indexes.
db._truncate(collection-identifier)
Truncates a collection identified by collection-identified. No error is
thrown if there is no such collection.
db._truncate(collection-name)
Truncates a collection named collection-name. No error is thrown if
there is no such collection.
Examples
Truncates a collection:
arangosh> col = db.example;
[ArangoCollection 1079396481, "example" (type document, status loaded)]
arangosh> col.save({ "Hello" : "World" });
{
"_id" : "example/1079724161",
"_rev" : "1079724161",
"_key" : "1079724161"
}
arangosh> col.count();
1
arangosh> db._truncate(col);
arangosh> col.count();
0
arangosh> col = db.example;
arangosh> col.save({ "Hello" : "World" });
arangosh> col.count();
arangosh> db._truncate(col);
arangosh> col.count();
Truncates a collection identified by name:
arangosh> col = db.example;
[ArangoCollection 1080379521, "example" (type document, status loaded)]
arangosh> col.save({ "Hello" : "World" });
{
"_id" : "example/1080707201",
"_rev" : "1080707201",
"_key" : "1080707201"
}
arangosh> col.count();
1
arangosh> db._truncate("example");
arangosh> col.count();
0
arangosh> col = db.example;
arangosh> col.save({ "Hello" : "World" });
arangosh> col.count();
arangosh> db._truncate("example");
arangosh> col.count();