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
Collection Methods
All
collection.all()
Fetches all documents from a collection and returns a cursor. You can use
toArray, next, or hasNext to access the result. The result
can be limited using the skip and limit operator.
Examples
Use toArray to get all documents at once:
arangosh> db.five.save({ name : "one" });
{
"_id" : "five/17778817",
"_rev" : "17778817",
"_key" : "17778817"
}
arangosh> db.five.save({ name : "two" });
{
"_id" : "five/17975425",
"_rev" : "17975425",
"_key" : "17975425"
}
arangosh> db.five.save({ name : "three" });
{
"_id" : "five/18172033",
"_rev" : "18172033",
"_key" : "18172033"
}
arangosh> db.five.save({ name : "four" });
{
"_id" : "five/18368641",
"_rev" : "18368641",
"_key" : "18368641"
}
arangosh> db.five.save({ name : "five" });
{
"_id" : "five/18565249",
"_rev" : "18565249",
"_key" : "18565249"
}
arangosh> db.five.all().toArray();
[
{
"name" : "four",
"_id" : "five/18368641",
"_rev" : "18368641",
"_key" : "18368641"
},
{
"name" : "three",
"_id" : "five/18172033",
"_rev" : "18172033",
"_key" : "18172033"
},
{
"name" : "five",
"_id" : "five/18565249",
"_rev" : "18565249",
"_key" : "18565249"
},
{
"name" : "two",
"_id" : "five/17975425",
"_rev" : "17975425",
"_key" : "17975425"
},
{
"name" : "one",
"_id" : "five/17778817",
"_rev" : "17778817",
"_key" : "17778817"
}
]
arangosh> db.five.save({ name : "one" });
arangosh> db.five.save({ name : "two" });
arangosh> db.five.save({ name : "three" });
arangosh> db.five.save({ name : "four" });
arangosh> db.five.save({ name : "five" });
arangosh> db.five.all().toArray();
Use limit to restrict the documents:
arangosh> db.five.save({ name : "one" });
{
"_id" : "five/19155073",
"_rev" : "19155073",
"_key" : "19155073"
}
arangosh> db.five.save({ name : "two" });
{
"_id" : "five/19351681",
"_rev" : "19351681",
"_key" : "19351681"
}
arangosh> db.five.save({ name : "three" });
{
"_id" : "five/19548289",
"_rev" : "19548289",
"_key" : "19548289"
}
arangosh> db.five.save({ name : "four" });
{
"_id" : "five/19744897",
"_rev" : "19744897",
"_key" : "19744897"
}
arangosh> db.five.save({ name : "five" });
{
"_id" : "five/19941505",
"_rev" : "19941505",
"_key" : "19941505"
}
arangosh> db.five.all().limit(2).toArray();
[
{
"name" : "one",
"_id" : "five/19155073",
"_rev" : "19155073",
"_key" : "19155073"
},
{
"name" : "three",
"_id" : "five/19548289",
"_rev" : "19548289",
"_key" : "19548289"
}
]
arangosh> db.five.save({ name : "one" });
arangosh> db.five.save({ name : "two" });
arangosh> db.five.save({ name : "three" });
arangosh> db.five.save({ name : "four" });
arangosh> db.five.save({ name : "five" });
arangosh> db.five.all().limit(2).toArray();
Query by example
collection.byExample(example)
Fetches all documents from a collection that match the specified
example and returns a cursor.
You can use toArray, next, or hasNext to access the
result. The result can be limited using the skip and limit
operator.
An attribute name of the form a.b is interpreted as attribute path,
not as attribute. If you use
{ a : { c : 1 } }
as example, then you will find all documents, such that the attribute
a contains a document of the form {c : 1 }. For example the document
{ a : { c : 1 }, b : 1 }
will match, but the document
{ a : { c : 1, b : 1 } }
will not.
However, if you use
{ a.c : 1 },
then you will find all documents, which contain a sub-document in a
that has an attribute c of value 1. Both the following documents
{ a : { c : 1 }, b : 1 } and
{ a : { c : 1, b : 1 } }
will match.
collection.byExample(path1, value1, ...)
As alternative you can supply an array of paths and values.
Examples
Use toArray to get all documents at once:
arangosh> db.users.save({ name: "Gerhard" });
{
"_id" : "users/20531329",
"_rev" : "20531329",
"_key" : "20531329"
}
arangosh> db.users.save({ name: "Helmut" });
{
"_id" : "users/20793473",
"_rev" : "20793473",
"_key" : "20793473"
}
arangosh> db.users.save({ name: "Angela" });
{
"_id" : "users/20990081",
"_rev" : "20990081",
"_key" : "20990081"
}
arangosh> db.users.all().toArray();
[
{
"name" : "Angela",
"_id" : "users/20990081",
"_rev" : "20990081",
"_key" : "20990081"
},
{
"name" : "Gerhard",
"_id" : "users/20531329",
"_rev" : "20531329",
"_key" : "20531329"
},
{
"name" : "Helmut",
"_id" : "users/20793473",
"_rev" : "20793473",
"_key" : "20793473"
}
]
arangosh> db.users.byExample({ "_id" : "users/20" }).toArray();
[ ]
arangosh> db.users.byExample({ "name" : "Gerhard" }).toArray();
[
{
"_id" : "users/20531329",
"_key" : "20531329",
"_rev" : "20531329",
"name" : "Gerhard"
}
]
arangosh> db.users.byExample({ "name" : "Helmut", "_id" : "users/15" }).toArray();
[ ]
arangosh> db.users.save({ name: "Gerhard" });
arangosh> db.users.save({ name: "Helmut" });
arangosh> db.users.save({ name: "Angela" });
arangosh> db.users.all().toArray();
arangosh> db.users.byExample({ "_id" : "users/20" }).toArray();
arangosh> db.users.byExample({ "name" : "Gerhard" }).toArray();
arangosh> db.users.byExample({ "name" : "Helmut", "_id" : "users/15" }).toArray();
Use next to loop over all documents:
arangosh> db.users.save({ name: "Gerhard" });
{
"_id" : "users/22235265",
"_rev" : "22235265",
"_key" : "22235265"
}
arangosh> db.users.save({ name: "Helmut" });
{
"_id" : "users/22497409",
"_rev" : "22497409",
"_key" : "22497409"
}
arangosh> db.users.save({ name: "Angela" });
{
"_id" : "users/22694017",
"_rev" : "22694017",
"_key" : "22694017"
}
arangosh> var a = db.users.byExample( {"name" : "Angela" } );
arangosh> while (a.hasNext()) print(a.next());
{
"_id" : "users/22694017",
"_key" : "22694017",
"_rev" : "22694017",
"name" : "Angela"
}
arangosh> db.users.save({ name: "Gerhard" });
arangosh> db.users.save({ name: "Helmut" });
arangosh> db.users.save({ name: "Angela" });
arangosh> var a = db.users.byExample( {"name" : "Angela" } );
arangosh> while (a.hasNext()) print(a.next());
First Example
collection.firstExample(example)
Returns the first document of a collection that matches the specified
example. If no such document exists, null will be returned.
The example has to be specified as paths and values.
See byExample for details.
collection.firstExample(path1, value1, ...)
As alternative you can supply an array of paths and values.
Examples
arangosh> db.users.firstExample("name", "Angela");
{
"_id" : "users/1089292417",
"_key" : "1089292417",
"_rev" : "1089292417",
"name" : "Angela"
}
arangosh> db.users.firstExample("name", "Angela");
Range
collection.range(attribute, left, right)
Returns all documents from a collection such that the attribute is
greater or equal than left and strictly less than right.
You can use toArray, next, or hasNext to access the
result. The result can be limited using the skip and limit
operator.
An attribute name of the form a.b is interpreted as attribute path,
not as attribute.
For range queries it is required that a skiplist index is present for the
queried attribute. If no skiplist index is present on the attribute, an
error will be thrown.
Note: the range simple query function is deprecated as of ArangoDB 2.6.
The function may be removed in future versions of ArangoDB. The preferred
way for retrieving documents from a collection within a specific range
is to use an AQL query as follows:
FOR doc IN @@collection
FILTER doc.value >= @left && doc.value < @right
LIMIT @skip, @limit
RETURN doc
Examples
Use toArray to get all documents at once:
arangosh> db.old.ensureIndex({ type: "skiplist", fields: [ "age" ] });
{
"id" : "old/23545985",
"type" : "skiplist",
"fields" : [
"age"
],
"unique" : false,
"sparse" : false,
"isNewlyCreated" : true,
"code" : 201
}
arangosh> db.old.save({ age: 15 });
{
"_id" : "old/23808129",
"_rev" : "23808129",
"_key" : "23808129"
}
arangosh> db.old.save({ age: 25 });
{
"_id" : "old/24004737",
"_rev" : "24004737",
"_key" : "24004737"
}
arangosh> db.old.save({ age: 30 });
{
"_id" : "old/24201345",
"_rev" : "24201345",
"_key" : "24201345"
}
arangosh> db.old.range("age", 10, 30).toArray();
[
{
"_id" : "old/23808129",
"_key" : "23808129",
"_rev" : "23808129",
"age" : 15
},
{
"_id" : "old/24004737",
"_key" : "24004737",
"_rev" : "24004737",
"age" : 25
}
]
arangosh> db.old.ensureIndex({ type: "skiplist", fields: [ "age" ] });
arangosh> db.old.save({ age: 15 });
arangosh> db.old.save({ age: 25 });
arangosh> db.old.save({ age: 30 });
arangosh> db.old.range("age", 10, 30).toArray();
Closed range
collection.closedRange(attribute, left, right)
Returns all documents of a collection such that the attribute is
greater or equal than left and less or equal than right.
You can use toArray, next, or hasNext to access the
result. The result can be limited using the skip and limit
operator.
An attribute name of the form a.b is interpreted as attribute path,
not as attribute.
Note: the closedRange simple query function is deprecated as of ArangoDB 2.6.
The function may be removed in future versions of ArangoDB. The preferred
way for retrieving documents from a collection within a specific range
is to use an AQL query as follows:
FOR doc IN @@collection
FILTER doc.value >= @left && doc.value <= @right
LIMIT @skip, @limit
RETURN doc
Examples
Use toArray to get all documents at once:
arangosh> db.old.ensureIndex({ type: "skiplist", fields: [ "age" ] });
{
"id" : "old/24922241",
"type" : "skiplist",
"fields" : [
"age"
],
"unique" : false,
"sparse" : false,
"isNewlyCreated" : true,
"code" : 201
}
arangosh> db.old.save({ age: 15 });
{
"_id" : "old/25184385",
"_rev" : "25184385",
"_key" : "25184385"
}
arangosh> db.old.save({ age: 25 });
{
"_id" : "old/25380993",
"_rev" : "25380993",
"_key" : "25380993"
}
arangosh> db.old.save({ age: 30 });
{
"_id" : "old/25577601",
"_rev" : "25577601",
"_key" : "25577601"
}
arangosh> db.old.closedRange("age", 10, 30).toArray();
[
{
"_id" : "old/25184385",
"_key" : "25184385",
"_rev" : "25184385",
"age" : 15
},
{
"_id" : "old/25380993",
"_key" : "25380993",
"_rev" : "25380993",
"age" : 25
},
{
"_id" : "old/25577601",
"_key" : "25577601",
"_rev" : "25577601",
"age" : 30
}
]
arangosh> db.old.ensureIndex({ type: "skiplist", fields: [ "age" ] });
arangosh> db.old.save({ age: 15 });
arangosh> db.old.save({ age: 25 });
arangosh> db.old.save({ age: 30 });
arangosh> db.old.closedRange("age", 10, 30).toArray();
Any
collection.any()
Returns a random document from the collection or null if none exists.
Count
collection.count()
Returns the number of living documents in the collection.
Examples
arangosh> db.users.count();
0
toArray
collection.toArray()
Converts the collection into an array of documents. Never use this call
in a production environment.
Document
collection.document(document)
The document method finds a document given its identifier or a document
object containing the _id or _key attribute. The method returns
the document if it can be found.
An error is thrown if _rev is specified but the document found has a
different revision already. An error is also thrown if no document exists
with the given _id or _key value.
Please note that if the method is executed on the arangod server (e.g. from
inside a Foxx application), an immutable document object will be returned
for performance reasons. It is not possible to change attributes of this
immutable object. To update or patch the returned document, it needs to be
cloned/copied into a regular JavaScript object first. This is not necessary
if the document method is called from out of arangosh or from any other
client.
collection.document(document-handle)
As before. Instead of document a document-handle can be passed as
first argument.
Examples
Returns the document for a document-handle:
arangosh> db.example.document("example/2873916");
{
"_id" : "example/2873916",
"_rev" : "1113278593",
"_key" : "2873916"
}
arangosh> db.example.document("example/2873916");
An error is raised if the document is unknown:
arangosh> db.example.document("example/4472917");
[ArangoError 1202: document not found]
An error is raised if the handle is invalid:
arangosh> db.example.document("");
[ArangoError 1205: illegal document handle]
Exists
collection.exists(document)
The exists method determines whether a document exists given its
identifier. Instead of returning the found document or an error, this
method will return either true or false. It can thus be used
for easy existence checks.
The document method finds a document given its identifier. It returns
the document. Note that the returned document contains two
pseudo-attributes, namely _id and _rev. _id contains the
document-handle and _rev the revision of the document.
No error will be thrown if the sought document or collection does not
exist.
Still this method will throw an error if used improperly, e.g. when called
with a non-document handle, a non-document, or when a cross-collection
request is performed.
collection.exists(document-handle)
As before. Instead of document a document-handle can be passed as
first argument.
Lookup By Keys
collection.documents(keys)
Looks up the documents in the specified collection using the array of keys
provided. All documents for which a matching key was specified in the keys
array and that exist in the collection will be returned.
Keys for which no document can be found in the underlying collection are ignored,
and no exception will be thrown for them.
Examples
arangosh> keys = [ ];
[ ]
arangosh> for (var i = 0; i < 10; ++i) {
........> db.example.insert({ _key: "test" + i, value: i });
........> keys.push("test" + i);
........> }
arangosh> db.example.documents(keys);
{
"documents" : [
{
"value" : 0,
"_id" : "example/test0",
"_rev" : "1092896897",
"_key" : "test0"
},
{
"value" : 1,
"_id" : "example/test1",
"_rev" : "1093093505",
"_key" : "test1"
},
{
"value" : 2,
"_id" : "example/test2",
"_rev" : "1093290113",
"_key" : "test2"
},
{
"value" : 3,
"_id" : "example/test3",
"_rev" : "1093486721",
"_key" : "test3"
},
{
"value" : 4,
"_id" : "example/test4",
"_rev" : "1093683329",
"_key" : "test4"
},
{
"value" : 5,
"_id" : "example/test5",
"_rev" : "1093879937",
"_key" : "test5"
},
{
"value" : 6,
"_id" : "example/test6",
"_rev" : "1094076545",
"_key" : "test6"
},
{
"value" : 7,
"_id" : "example/test7",
"_rev" : "1094273153",
"_key" : "test7"
},
{
"value" : 8,
"_id" : "example/test8",
"_rev" : "1094469761",
"_key" : "test8"
},
{
"value" : 9,
"_id" : "example/test9",
"_rev" : "1094666369",
"_key" : "test9"
}
]
}
arangosh> keys = [ ];
arangosh> for (var i = 0; i < 10; ++i) {
........> db.example.insert({ _key: "test" + i, value: i });
........> keys.push("test" + i);
........> }
arangosh> db.example.documents(keys);
Insert
collection.insert(data)
Creates a new document in the collection from the given data. The
data must be an object.
The method returns a document with the attributes _id and _rev.
The attribute _id contains the document handle of the newly created
document, the attribute _rev contains the document revision.
collection.insert(data, waitForSync)
Creates a new document in the collection from the given data as
above. The optional waitForSync parameter can be used to force
synchronization of the document creation operation to disk even in case
that the waitForSync flag had been disabled for the entire collection.
Thus, the waitForSync parameter can be used to force synchronization
of just specific operations. To use this, set the waitForSync parameter
to true. If the waitForSync parameter is not specified or set to
false, then the collection’s default waitForSync behavior is
applied. The waitForSync parameter cannot be used to disable
synchronization for collections that have a default waitForSync value
of true.
Note: since ArangoDB 2.2, insert is an alias for save.
Examples
arangosh> db.example.insert({ Hello : "World" });
{
"_id" : "example/1111115905",
"_rev" : "1111115905",
"_key" : "1111115905"
}
arangosh> db.example.insert({ Hello : "World" }, true);
{
"_id" : "example/1111312513",
"_rev" : "1111312513",
"_key" : "1111312513"
}
arangosh> db.example.insert({ Hello : "World" });
arangosh> db.example.insert({ Hello : "World" }, true);
Replace
collection.replace(document, data)
Replaces an existing document. The document must be a document in
the current collection. This document is then replaced with the
data given as second argument.
The method returns a document with the attributes _id, _rev and
{_oldRev. The attribute _id contains the document handle of the
updated document, the attribute _rev contains the document revision of
the updated document, the attribute _oldRev contains the revision of
the old (now replaced) document.
If there is a conflict, i. e. if the revision of the document does not
match the revision in the collection, then an error is thrown.
collection.replace(document, data, true)
or
collection.replace(document, data, overwrite: true)
As before, but in case of a conflict, the conflict is ignored and the old
document is overwritten.
collection.replace(document, data, true, waitForSync)
or
collection.replace(document, data, overwrite: true, waitForSync: true or false)
The optional waitForSync parameter can be used to force
synchronization of the document replacement operation to disk even in case
that the waitForSync flag had been disabled for the entire collection.
Thus, the waitForSync parameter can be used to force synchronization
of just specific operations. To use this, set the waitForSync parameter
to true. If the waitForSync parameter is not specified or set to
false, then the collection’s default waitForSync behavior is
applied. The waitForSync parameter cannot be used to disable
synchronization for collections that have a default waitForSync value
of true.
collection.replace(document-handle, data)
As before. Instead of document a document-handle can be passed as
first argument.
Examples
Create and update a document:
arangosh> a1 = db.example.insert({ a : 1 });
{
"_id" : "example/1117341825",
"_rev" : "1117341825",
"_key" : "1117341825"
}
arangosh> a2 = db.example.replace(a1, { a : 2 });
{
"_id" : "example/1117341825",
"_rev" : "1117538433",
"_key" : "1117341825"
}
arangosh> a3 = db.example.replace(a1, { a : 3 });
[ArangoError 1200: precondition failed]
arangosh> a1 = db.example.insert({ a : 1 });
arangosh> a2 = db.example.replace(a1, { a : 2 });
arangosh> a3 = db.example.replace(a1, { a : 3 });
Use a document handle:
arangosh> a1 = db.example.insert({ a : 1 });
{
"_id" : "example/1118455937",
"_rev" : "1118455937",
"_key" : "1118455937"
}
arangosh> a2 = db.example.replace("example/3903044", { a : 2 });
{
"_id" : "example/3903044",
"_rev" : "1118652545",
"_key" : "3903044"
}
arangosh> a1 = db.example.insert({ a : 1 });
arangosh> a2 = db.example.replace("example/3903044", { a : 2 });
Update
collection.update(document, data, overwrite, keepNull, waitForSync)
or
collection.update(document, data,
overwrite: true or false, keepNull: true or false, waitForSync: true or false)
Updates an existing document. The document must be a document in
the current collection. This document is then patched with the
data given as second argument. The optional overwrite parameter can
be used to control the behavior in case of version conflicts (see below).
The optional keepNull parameter can be used to modify the behavior when
handling null values. Normally, null values are stored in the
database. By setting the keepNull parameter to false, this behavior
can be changed so that all attributes in data with null values will
be removed from the target document.
The optional waitForSync parameter can be used to force
synchronization of the document update operation to disk even in case
that the waitForSync flag had been disabled for the entire collection.
Thus, the waitForSync parameter can be used to force synchronization
of just specific operations. To use this, set the waitForSync parameter
to true. If the waitForSync parameter is not specified or set to
false, then the collection’s default waitForSync behavior is
applied. The waitForSync parameter cannot be used to disable
synchronization for collections that have a default waitForSync value
of true.
The method returns a document with the attributes _id, _rev and
_oldRev. The attribute _id contains the document handle of the
updated document, the attribute _rev contains the document revision of
the updated document, the attribute _oldRev contains the revision of
the old (now replaced) document.
If there is a conflict, i. e. if the revision of the document does not
match the revision in the collection, then an error is thrown.
collection.update(document, data, true)
As before, but in case of a conflict, the conflict is ignored and the old
document is overwritten.
collection.update(document-handle, data)`
As before. Instead of document a document-handle can be passed as
first argument.
Examples
Create and update a document:
arangosh> a1 = db.example.insert({"a" : 1});
{
"_id" : "example/1119176833",
"_rev" : "1119176833",
"_key" : "1119176833"
}
arangosh> a2 = db.example.update(a1, {"b" : 2, "c" : 3});
{
"_id" : "example/1119176833",
"_rev" : "1119570049",
"_key" : "1119176833"
}
arangosh> a3 = db.example.update(a1, {"d" : 4});
[ArangoError 1200: precondition failed]
arangosh> a4 = db.example.update(a2, {"e" : 5, "f" : 6 });
{
"_id" : "example/1119176833",
"_rev" : "1120225409",
"_key" : "1119176833"
}
arangosh> db.example.document(a4);
{
"a" : 1,
"b" : 2,
"c" : 3,
"e" : 5,
"f" : 6,
"_id" : "example/1119176833",
"_rev" : "1120225409",
"_key" : "1119176833"
}
arangosh> a5 = db.example.update(a4, {"a" : 1, c : 9, e : 42 });
{
"_id" : "example/1119176833",
"_rev" : "1120487553",
"_key" : "1119176833"
}
arangosh> db.example.document(a5);
{
"a" : 1,
"b" : 2,
"c" : 9,
"e" : 42,
"f" : 6,
"_id" : "example/1119176833",
"_rev" : "1120487553",
"_key" : "1119176833"
}
arangosh> a1 = db.example.insert({"a" : 1});
arangosh> a2 = db.example.update(a1, {"b" : 2, "c" : 3});
arangosh> a3 = db.example.update(a1, {"d" : 4});
arangosh> a4 = db.example.update(a2, {"e" : 5, "f" : 6 });
arangosh> db.example.document(a4);
arangosh> a5 = db.example.update(a4, {"a" : 1, c : 9, e : 42 });
arangosh> db.example.document(a5);
Use a document handle:
arangosh> a1 = db.example.insert({"a" : 1});
{
"_id" : "example/1121339521",
"_rev" : "1121339521",
"_key" : "1121339521"
}
arangosh> a2 = db.example.update("example/18612115", { "x" : 1, "y" : 2 });
{
"_id" : "example/18612115",
"_rev" : "1121732737",
"_key" : "18612115"
}
arangosh> a1 = db.example.insert({"a" : 1});
arangosh> a2 = db.example.update("example/18612115", { "x" : 1, "y" : 2 });
Use the keepNull parameter to remove attributes with null values:
arangosh> db.example.insert({"a" : 1});
{
"_id" : "example/1124550785",
"_rev" : "1124550785",
"_key" : "1124550785"
}
arangosh> db.example.update("example/19988371", { "b" : null, "c" : null, "d" : 3 });
{
"_id" : "example/19988371",
"_rev" : "1125009537",
"_key" : "19988371"
}
arangosh> db.example.document("example/19988371");
{
"b" : null,
"c" : null,
"d" : 3,
"_id" : "example/19988371",
"_rev" : "1125009537",
"_key" : "19988371"
}
arangosh> db.example.update("example/19988371", { "a" : null }, false, false);
{
"_id" : "example/19988371",
"_rev" : "1125271681",
"_key" : "19988371"
}
arangosh> db.example.document("example/19988371");
{
"b" : null,
"c" : null,
"d" : 3,
"_id" : "example/19988371",
"_rev" : "1125271681",
"_key" : "19988371"
}
arangosh> db.example.update("example/19988371", { "b" : null, "c": null, "d" : null }, false, false);
{
"_id" : "example/19988371",
"_rev" : "1125533825",
"_key" : "19988371"
}
arangosh> db.example.document("example/19988371");
{
"_id" : "example/19988371",
"_rev" : "1125533825",
"_key" : "19988371"
}
arangosh> db.example.insert({"a" : 1});
arangosh> db.example.update("example/19988371", { "b" : null, "c" : null, "d" : 3 });
arangosh> db.example.document("example/19988371");
arangosh> db.example.update("example/19988371", { "a" : null }, false, false);
arangosh> db.example.document("example/19988371");
arangosh> db.example.update("example/19988371", { "b" : null, "c": null, "d" : null }, false, false);
arangosh> db.example.document("example/19988371");
Patching array values:
arangosh> db.example.insert({"a" : { "one" : 1, "two" : 2, "three" : 3 }, "b" : { }});
{
"_id" : "example/1122846849",
"_rev" : "1122846849",
"_key" : "1122846849"
}
arangosh> db.example.update("example/20774803", {"a" : { "four" : 4 }, "b" : { "b1" : 1 }});
{
"_id" : "example/20774803",
"_rev" : "1123371137",
"_key" : "20774803"
}
arangosh> db.example.document("example/20774803");
{
"a" : {
"four" : 4
},
"b" : {
"b1" : 1
},
"_id" : "example/20774803",
"_rev" : "1123371137",
"_key" : "20774803"
}
arangosh> db.example.update("example/20774803", { "a" : { "one" : null }, "b" : null }, false, false);
{
"_id" : "example/20774803",
"_rev" : "1123698817",
"_key" : "20774803"
}
arangosh> db.example.document("example/20774803");
{
"a" : {
"four" : 4
},
"_id" : "example/20774803",
"_rev" : "1123698817",
"_key" : "20774803"
}
arangosh> db.example.insert({"a" : { "one" : 1, "two" : 2, "three" : 3 }, "b" : { }});
arangosh> db.example.update("example/20774803", {"a" : { "four" : 4 }, "b" : { "b1" : 1 }});
arangosh> db.example.document("example/20774803");
arangosh> db.example.update("example/20774803", { "a" : { "one" : null }, "b" : null }, false, false);
arangosh> db.example.document("example/20774803");
Remove
collection.remove(document)
Removes a document. If there is revision mismatch, then an error is thrown.
collection.remove(document, true)
Removes a document. If there is revision mismatch, then mismatch is ignored
and document is deleted. The function returns true if the document
existed and was deleted. It returns false, if the document was already
deleted.
collection.remove(document, true, waitForSync)
The optional waitForSync parameter can be used to force synchronization
of the document deletion operation to disk even in case that the
waitForSync flag had been disabled for the entire collection. Thus,
the waitForSync parameter can be used to force synchronization of just
specific operations. To use this, set the waitForSync parameter to
true. If the waitForSync parameter is not specified or set to
false, then the collection’s default waitForSync behavior is
applied. The waitForSync parameter cannot be used to disable
synchronization for collections that have a default waitForSync value
of true.
collection.remove(document-handle, data)
As before. Instead of document a document-handle can be passed as
first argument.
Examples
Remove a document:
arangosh> a1 = db.example.insert({ a : 1 });
{
"_id" : "example/1106528385",
"_rev" : "1106528385",
"_key" : "1106528385"
}
arangosh> db.example.document(a1);
{
"a" : 1,
"_id" : "example/1106528385",
"_rev" : "1106528385",
"_key" : "1106528385"
}
arangosh> db.example.remove(a1);
true
arangosh> db.example.document(a1);
[ArangoError 1202: document not found]
arangosh> a1 = db.example.insert({ a : 1 });
arangosh> db.example.document(a1);
arangosh> db.example.remove(a1);
arangosh> db.example.document(a1);
Remove a document with a conflict:
arangosh> a1 = db.example.insert({ a : 1 });
{
"_id" : "example/1107380353",
"_rev" : "1107380353",
"_key" : "1107380353"
}
arangosh> a2 = db.example.replace(a1, { a : 2 });
{
"_id" : "example/1107380353",
"_rev" : "1107576961",
"_key" : "1107380353"
}
arangosh> db.example.remove(a1);
[ArangoError 1200: precondition failed]
arangosh> db.example.remove(a1, true);
true
arangosh> db.example.document(a1);
[ArangoError 1202: document not found]
arangosh> a1 = db.example.insert({ a : 1 });
arangosh> a2 = db.example.replace(a1, { a : 2 });
arangosh> db.example.remove(a1);
arangosh> db.example.remove(a1, true);
arangosh> db.example.document(a1);
Remove By Keys
collection.removeByKeys(keys)
Looks up the documents in the specified collection using the array of keys
provided, and removes all documents from the collection whose keys are
contained in the keys array. Keys for which no document can be found in
the underlying collection are ignored, and no exception will be thrown for
them.
The method will return an object containing the number of removed documents
in the removed sub-attribute, and the number of not-removed/ignored
documents in the ignored sub-attribute.
Examples
arangosh> keys = [ ];
[ ]
arangosh> for (var i = 0; i < 10; ++i) {
........> db.example.insert({ _key: "test" + i, value: i });
........> keys.push("test" + i);
........> }
arangosh> db.example.removeByKeys(keys);
{
"removed" : 10,
"ignored" : 0
}
arangosh> keys = [ ];
arangosh> for (var i = 0; i < 10; ++i) {
........> db.example.insert({ _key: "test" + i, value: i });
........> keys.push("test" + i);
........> }
arangosh> db.example.removeByKeys(keys);
Remove By Example
collection.removeByExample(example)
Removes all documents matching an example.
collection.removeByExample(document, waitForSync)
The optional waitForSync parameter can be used to force synchronization
of the document deletion operation to disk even in case that the
waitForSync flag had been disabled for the entire collection. Thus,
the waitForSync parameter can be used to force synchronization of just
specific operations. To use this, set the waitForSync parameter to
true. If the waitForSync parameter is not specified or set to
false, then the collection’s default waitForSync behavior is
applied. The waitForSync parameter cannot be used to disable
synchronization for collections that have a default waitForSync value
of true.
collection.removeByExample(document, waitForSync, limit)
The optional limit parameter can be used to restrict the number of
removals to the specified value. If limit is specified but less than the
number of documents in the collection, it is undefined which documents are
removed.
Examples
arangosh> db.example.removeByExample( {Hello : "world"} );
1
Replace By Example
collection.replaceByExample(example, newValue)
Replaces all documents matching an example with a new document body.
The entire document body of each document matching the example will be
replaced with newValue. The document meta-attributes such as _id,
_key, _from, _to will not be replaced.
collection.replaceByExample(document, newValue, waitForSync)
The optional waitForSync parameter can be used to force synchronization
of the document replacement operation to disk even in case that the
waitForSync flag had been disabled for the entire collection. Thus,
the waitForSync parameter can be used to force synchronization of just
specific operations. To use this, set the waitForSync parameter to
true. If the waitForSync parameter is not specified or set to
false, then the collection’s default waitForSync behavior is
applied. The waitForSync parameter cannot be used to disable
synchronization for collections that have a default waitForSync value
of true.
collection.replaceByExample(document, newValue, waitForSync, limit)
The optional limit parameter can be used to restrict the number of
replacements to the specified value. If limit is specified but less than
the number of documents in the collection, it is undefined which documents are
replaced.
Examples
arangosh> db.example.save({ Hello : "world" });
{
"_id" : "example/444614785",
"_rev" : "444614785",
"_key" : "444614785"
}
arangosh> db.example.replaceByExample({ Hello: "world" }, {Hello: "mars"}, false, 5);
1
arangosh> db.example.save({ Hello : "world" });
arangosh> db.example.replaceByExample({ Hello: "world" }, {Hello: "mars"}, false, 5);
Update By Example
collection.updateByExample(example, newValue)
Partially updates all documents matching an example with a new document body.
Specific attributes in the document body of each document matching the
example will be updated with the values from newValue.
The document meta-attributes such as _id, _key, _from,
_to cannot be updated.
Partial update could also be used to append new fields,
if there were no old field with same name.
collection.updateByExample(document, newValue, keepNull, waitForSync)
The optional keepNull parameter can be used to modify the behavior when
handling null values. Normally, null values are stored in the
database. By setting the keepNull parameter to false, this behavior
can be changed so that all attributes in data with null values will
be removed from the target document.
The optional waitForSync parameter can be used to force synchronization
of the document replacement operation to disk even in case that the
waitForSync flag had been disabled for the entire collection. Thus,
the waitForSync parameter can be used to force synchronization of just
specific operations. To use this, set the waitForSync parameter to
true. If the waitForSync parameter is not specified or set to
false, then the collection’s default waitForSync behavior is
applied. The waitForSync parameter cannot be used to disable
synchronization for collections that have a default waitForSync value
of true.
collection.updateByExample(document, newValue, keepNull, waitForSync, limit)
The optional limit parameter can be used to restrict the number of
updates to the specified value. If limit is specified but less than
the number of documents in the collection, it is undefined which documents are
updated.
collection.updateByExample(document, newValue, options)
Using this variant, the options for the operation can be passed using
an object with the following sub-attributes:
- keepNull
- waitForSync
- limit
- mergeObjects
Examples
arangosh> db.example.save({ Hello : "world", foo : "bar" });
{
"_id" : "example/445532289",
"_rev" : "445532289",
"_key" : "445532289"
}
arangosh> db.example.updateByExample({ Hello: "world" }, { Hello: "foo", World: "bar" }, false);
1
arangosh> db.example.byExample({ Hello: "foo" }).toArray()
[
{
"_id" : "example/445532289",
"_key" : "445532289",
"_rev" : "445859969",
"Hello" : "foo",
"foo" : "bar",
"World" : "bar"
}
]
arangosh> db.example.save({ Hello : "world", foo : "bar" });
arangosh> db.example.updateByExample({ Hello: "world" }, { Hello: "foo", World: "bar" }, false);
arangosh> db.example.byExample({ Hello: "foo" }).toArray()
First
collection.first(count)
The first method returns the n first documents from the collection, in
order of document insertion/update time.
If called with the count argument, the result is a list of up to
count documents. If count is bigger than the number of documents
in the collection, then the result will contain as many documents as there
are in the collection.
The result list is ordered, with the “oldest” documents being positioned at
the beginning of the result list.
When called without an argument, the result is the first document from the
collection. If the collection does not contain any documents, the result
returned is null.
Note: this method is not supported in sharded collections with more than
one shard.
Examples
arangosh> db.example.first(1);
[
{
"_id" : "example/1109608577",
"_key" : "1109608577",
"_rev" : "1109608577",
"Hello" : "world"
}
]
arangosh> db.example.first(1);
arangosh> db.example.first();
{
"_id" : "example/1110526081",
"_key" : "1110526081",
"_rev" : "1110526081",
"Hello" : "world"
}
arangosh> db.example.first();
Last
collection.last(count)
The last method returns the n last documents from the collection, in
order of document insertion/update time.
If called with the count argument, the result is a list of up to
count documents. If count is bigger than the number of documents
in the collection, then the result will contain as many documents as there
are in the collection.
The result list is ordered, with the “latest” documents being positioned at
the beginning of the result list.
When called without an argument, the result is the last document from the
collection. If the collection does not contain any documents, the result
returned is null.
Note: this method is not supported in sharded collections with more than
one shard.
Examples
arangosh> db.example.last(2);
[
{
"_id" : "example/1112164481",
"_key" : "1112164481",
"_rev" : "1112164481",
"Foo" : "bar"
},
{
"_id" : "example/1111836801",
"_key" : "1111836801",
"_rev" : "1111836801",
"Hello" : "world"
}
]
arangosh> db.example.last(2);
arangosh> db.example.last(1);
[
{
"_id" : "example/1112754305",
"_key" : "1112754305",
"_rev" : "1112754305",
"Hello" : "world"
}
]
arangosh> db.example.last(1);
Collection type
collection.type()
Returns the type of a collection. Possible values are:
- 2: document collection
- 3: edge collection
Get the Version of ArangoDB
db._version()
Returns the server version string. Note that this is not the version of the
database.
Examples
arangosh> require("internal").db._version();
2.8.11
Misc
collection.edges(vertex-id)
Returns all edges connected to the vertex specified by vertex-id.
collection.inEdges(vertex-id)
Returns inbound edges connected to the vertex specified by vertex-id.
collection.outEdges(vertex-id)
Returns outbound edges connected to the vertex specified by vertex-id.
iterates over some elements of a collectioncollection.iterate(iterator, options)
Iterates over some elements of the collection and apply the function
iterator to the elements. The function will be called with the
document as first argument and the current number (starting with 0)
as second argument.
options must be an object with the following attributes:
- limit (optional, default none): use at most limit documents.
- probability (optional, default all): a number between 0 and
1. Documents are chosen with this probability.
Examples
arangosh> for (i = -90; i <= 90; i += 10) {
........> for (j = -180; j <= 180; j += 10) {
........> db.example.save({ name : "Name/" + i + "/" + j,
........> home : [ i, j ],
........> work : [ -i, -j ] });
........> }
........> }
........>
arangosh> db.example.ensureIndex({ type: "geo", fields: [ "home" ] });
{
"id" : "example/1075202177",
"type" : "geo1",
"fields" : [
"home"
],
"geoJson" : false,
"constraint" : false,
"unique" : false,
"ignoreNull" : true,
"sparse" : true,
"isNewlyCreated" : true,
"code" : 201
}
arangosh> items = db.example.getIndexes().map(function(x) { return x.id; });
........> db.example.index(items[1]);
[
"example/0",
"example/1075202177"
]
arangosh> for (i = -90; i <= 90; i += 10) {
........> for (j = -180; j <= 180; j += 10) {
........> db.example.save({ name : "Name/" + i + "/" + j,
........> home : [ i, j ],
........> work : [ -i, -j ] });
........> }
........> }
........>
arangosh> db.example.ensureIndex({ type: "geo", fields: [ "home" ] });
arangosh> items = db.example.getIndexes().map(function(x) { return x.id; });
........> db.example.index(items[1]);
edge.setProperty(name, value)
Changes or sets the property name an edges to value.