test di ArangoDB V.3.1.7
ADD USER
require("org/arangodb/users").save("sammy", "password");
INSERT
FOR ini IN [
{ name:
"Isabela", gender: "m" },
{ name:
"John Doe", gender: "m" , Alamat:"Cipaku"},
{ name:
"Jane Smith", gender: "f" ,
No_hp:"081312651651"},
{ name: "Anu", gender: "f" }
]
INSERT ini
INTO Mahasiswa
INSERT WITH
_key
INSERT {
_key:
"GilbertoGil",
firstName:
"Gilberto",
name:
"Gil",
city:
"Fortalezza"
} IN users
INSERT DARI
TABEL LAIN
FOR u IN users
INSERT u IN backupUser
//UPDATE
UPDATE "indonesia" WITH {
negara:
"INDONESIA RAYA"
} IN negara
Update… “indonesia” -> _key-nya
////
UPDATE
"Key_nya" WITH {
status:
"active",
location:
"Beijing"
} IN users
//REMOVE
REMOVE
"id_nya" IN users
or
REMOVE { _key:
"key_nya" } IN users
REMOVE
BERDASARKAN FILTER
FOR
u IN users
FILTER u.active == true && u.age >= 35
&& u.age <= 37
REMOVE u IN users
HITUNG RECORD
-
RETURN
LENGTH(provinsi)
-
FOR
doc IN kab
COLLECT
WITH COUNT INTO length
RETURN
length
//CONTOH
PERULANGAN INSERT + KONDISI
FOR i IN
1..1000
INSERT {
id:
100000 + i,
age:
18 + FLOOR(RAND() *
25),
name: CONCAT(
'test', TO_STRING(i)),
active:
false,
gender: i %
2 ==
0 ?
'male' :
'female'
} IN users
//
FOR i IN 1..1000
INSERT {
nama:
CONCAT("namanya", i),
jenis_kelamin: (i % 2 == 0 ? "Laki-laki" :
"Perempuan")
}
INTO
Mahasiswa
//CONTOH ADA
KEY
FOR i IN
1..1000
INSERT {
_key:
CONCAT('prov_', i),
namaProv:
"provinsi"+i,
_from: CONCAT('prov_', i)
walikota:
"walikota"+i
} INTO
provinsi OPTIONS { ignoreErrors: true } // Abaikan Error
//CONTOH
FOR i IN 1..1000
INSERT {
name: CONCAT("test", i),
gender: (i % 2 == 0 ? "f" :
"m")
}
INTO users
SUB QUERY
FOR p IN persons
COLLECT city = p.city INTO g
RETURN {
city : city,
numPersons : LENGTH(g),
maxRating: MAX(
FOR r IN g
RETURN r.p.rating
)}
//Select semua
collection Person
FOR u IN person RETURN u
FILTERING
FOR u IN users
LIMIT
0,
3
RETURN u
//limit 3
FOR u IN users
LIMIT
0,
3
RETURN {
"user" : {
"isActive" : u.active ?
"yes" :
"no",
"name" : u.name
}
}
//FILTER + Pake
Sortir
FOR u IN users
FILTER u.active ==
true && u.age >=
30
SORT u.age DESC
LIMIT
0,
5
RETURN {
"age" : u.age,
"name" : u.name
}
//SORTIR
FOR peg IN pegawai
SORT u.nama DESC
RETURN peg
//FILTER
COLLECTION
FOR user IN Mahasiswa
FILTER user.name=="Isabela" or
user.name=="Dini"
RETURN user
FOR user IN Mahasiswa
FILTER user.jenis_kelamin=="Laki-laki"
RETURN user
//select
berdaasar ID
FOR u IN person FILTER u._id == “id_nya” RETURN u
//Berdasarkan
gender dan umur
FOR u IN users FILTER u.age > 19 &&
u.gender == 'm' RETURN u"
// cari data
orang yg lebih dari Gender male && 30 (hasil true/false)
FOR u IN person SORT u.age RETURN
{ name :
u.name, age:u.age, lebihdari : u.age > 30 }
//OPERTOR ||
ATAU
FOR u IN kenal
FILTER u._from=='person/135130' ||
u._from=='person/136632'
RETURN u
QUERY GRAPH
//yg keluar
dari 136632
FOR v, e, p IN OUTBOUND 'person/136632' GRAPH
'GraphKu' RETURN p
//yg masuk ke
135158
FOR v, e, p IN INBOUND 'person/135158' GRAPH
'GraphKu' RETURN p
//yg masuk
maupun keluar
FOR v, e, aa IN ANY 'person/135158' GRAPH 'GraphKu' RETURN aa
// memilih
kaditu kadieu na pada Edges
For X IN kenal
FILTER X._from=="person/136632" ||
X._to=="person/135158" || X._from=="person/151243"
return X
for anu IN hub
FILTER anu._to=="provinsi/180084" ||
anu._to=="provinsi/180182"
return anu
//FILTER
INBOUND/OUTBOUND Berdasar id_Edges pada graph
for e,v, anu
IN 1..5 INBOUND 'negara/192174'
graph 'GraphHub'
FILTER anu.edges[0]._id=="hub/192223"
return anu
GROUPING
FOR u IN kab
COLLECT
namanya = u.nama
RETURN
namanya
// Fungsi
Distinct
FOR u IN users
RETURN DISTINCT u.age
//FILTER dan
sortir
FOR u IN users
FILTER u.active ==
true
COLLECT age = u.age INTO usersByAge
SORT age DESC LIMIT
0,
5
RETURN {
age,
users: usersByAge[*].u.name
}
//hasil di
bawah
[
{
"age":
37,
"users": [
"John",
"Sophia" ] },
{
"age":
36,
"users": [
"Fred",
"Emma" ] },
{
"age":
34,
"users": [
"Madison" ] },
{
"age":
33,
"users": [
"Chloe",
"Michael" ] },
{
"age":
32,
"users": [
"Alexander" ] }
]
//AMBIL DATE
RETURN DATE_FORMAT(DATE_NOW(),
"%dd/%MM/%yyyy")
// DAY OF WEEK
return DATE_DAYOFWEEK(DATE_NOW())
//AMBIL YEAR,DAY,MONTH,MINUTE,HOUR,SECOND,DAYOFYEAR
return DATE_YEAR(DATE_NOW())
//HITUNG
JUMLAH HARI BULAN INI
return DATE_DAYS_IN_MONTH(DATE_NOW())
DURUNG NGERTI
WITH provinsi,negara
FOR v, e, p IN ANY 'negara/indonesia' GRAPH
'graphHub'
RETURN { v,
e, p }
FOR v, e, p IN 0..10 INBOUND
"provinsi/prov_1" graph 'graphHubKab'
RETURN
CONCAT_SEPARATOR(" -> ", p.vertices[*].naon)
SUMBER : https://www.arangodb.com/why-arangodb/sql-aql-comparison/
docs.arangodb.com