😎
ArangoDBのCollectionにNodeJSでアクセスする
ArangoDBのCollectionにNodeJSでアクセスするまでを記載
Collectionのデータ内容
ここにサンプルデータがあるので持ってくる
データ追加方法
COLLECTIONSからAdd Collectionをクリック
サンプルデータに従ってNameをusersにしてSaveをクリック
usersができてるのでクリック
右上の方にある+をクリック
サンプルデータである
[
{ "id": 100, "name": "John", "age": 37, "active": true, "gender": "m" },
{ "id": 101, "name": "Fred", "age": 36, "active": true, "gender": "m" },
{ "id": 102, "name": "Jacob", "age": 35, "active": false, "gender": "m" },
{ "id": 103, "name": "Ethan", "age": 34, "active": false, "gender": "m" },
{ "id": 104, "name": "Michael", "age": 33, "active": true, "gender": "m" },
{ "id": 105, "name": "Alexander", "age": 32, "active": true, "gender": "m" },
{ "id": 106, "name": "Daniel", "age": 31, "active": true, "gender": "m" },
{ "id": 107, "name": "Anthony", "age": 30, "active": true, "gender": "m" },
{ "id": 108, "name": "Jim", "age": 29, "active": true, "gender": "m" },
{ "id": 109, "name": "Diego", "age": 28, "active": true, "gender": "m" },
{ "id": 200, "name": "Sophia", "age": 37, "active": true, "gender": "f" },
{ "id": 201, "name": "Emma", "age": 36, "active": true, "gender": "f" },
{ "id": 202, "name": "Olivia", "age": 35, "active": false, "gender": "f" },
{ "id": 203, "name": "Madison", "age": 34, "active": true, "gender": "f" },
{ "id": 204, "name": "Chloe", "age": 33, "active": true, "gender": "f" },
{ "id": 205, "name": "Eva", "age": 32, "active": false, "gender": "f" },
{ "id": 206, "name": "Abigail", "age": 31, "active": true, "gender": "f" },
{ "id": 207, "name": "Isabella", "age": 30, "active": true, "gender": "f" },
{ "id": 208, "name": "Mary", "age": 29, "active": true, "gender": "f" },
{ "id": 209, "name": "Mariah", "age": 28, "active": true, "gender": "f" }
]
をDocument bodyコピペしてCreateをクリック
画面がリフレッシュされないので適当にリロードなりしてデータ追加されてるのを確認する
同じように
[
{ "from": 209, "to": 205, "type": "friend" },
{ "from": 206, "to": 108, "type": "friend" },
{ "from": 202, "to": 204, "type": "friend" },
{ "from": 200, "to": 100, "type": "friend" },
{ "from": 205, "to": 101, "type": "friend" },
{ "from": 209, "to": 203, "type": "friend" },
{ "from": 200, "to": 203, "type": "friend" },
{ "from": 100, "to": 208, "type": "friend" },
{ "from": 101, "to": 209, "type": "friend" },
{ "from": 206, "to": 102, "type": "friend" },
{ "from": 104, "to": 100, "type": "friend" },
{ "from": 104, "to": 108, "type": "friend" },
{ "from": 108, "to": 209, "type": "friend" },
{ "from": 206, "to": 106, "type": "friend" },
{ "from": 204, "to": 105, "type": "friend" },
{ "from": 208, "to": 207, "type": "friend" },
{ "from": 102, "to": 108, "type": "friend" },
{ "from": 207, "to": 203, "type": "friend" },
{ "from": 203, "to": 106, "type": "friend" },
{ "from": 202, "to": 108, "type": "friend" },
{ "from": 201, "to": 203, "type": "friend" },
{ "from": 105, "to": 100, "type": "friend" },
{ "from": 100, "to": 109, "type": "friend" },
{ "from": 207, "to": 109, "type": "friend" },
{ "from": 103, "to": 203, "type": "friend" },
{ "from": 208, "to": 104, "type": "friend" },
{ "from": 105, "to": 104, "type": "friend" },
{ "from": 103, "to": 208, "type": "friend" },
{ "from": 203, "to": 107, "type": "boyfriend" },
{ "from": 107, "to": 203, "type": "girlfriend" },
{ "from": 208, "to": 109, "type": "boyfriend" },
{ "from": 109, "to": 208, "type": "girlfriend" },
{ "from": 106, "to": 205, "type": "girlfriend" },
{ "from": 205, "to": 106, "type": "boyfriend" },
{ "from": 103, "to": 209, "type": "girlfriend" },
{ "from": 209, "to": 103, "type": "boyfriend" },
{ "from": 201, "to": 102, "type": "boyfriend" },
{ "from": 102, "to": 201, "type": "girlfriend" },
{ "from": 206, "to": 100, "type": "boyfriend" },
{ "from": 100, "to": 206, "type": "girlfriend" }
]
をrelationsとしてコレクション登録&データ追加する
最終的にusersとrelationsというコレクション名があればOK
NodeJSプログラム
使用するライブラリ
arangojsライブラリインストール
npm install --save arangojs
## - or -
yarn add arangojs
// TS: import { Database, aql } from "arangojs";
const { Database, aql } = require("arangojs");
const db = new Database({
url: "ArangoDBのURL",
databaseName: "_system",
auth: { username: "ユーザーID", password: "パスワード" },
});
const Users = db.collection("users");
async function main() {
try {
const users = await db.query(aql`
FOR user IN ${Users}
FILTER user.name == "Alexander"
RETURN user
`);
for await (const user of users) {
console.log(user);
}
} catch (err) {
console.error(err.message);
}
}
main();
ArangoDBのURL
にはhttp://ArangoDBが動作してるIPアドレス:8529
になります
databaseName
は_system
としてますが、画像右上にDB:_SYSTEMと書いてるところから判断できます(URLにも_db/_system
と記載してるので判断できるかと)
実行結果
{
_key: '42214',
_id: 'users/42214',
_rev: '_c9FPyEa--D',
id: 105,
name: 'Alexander',
age: 32,
active: true,
gender: 'm'
}
※_key
とか_id
とか_rev
はArangoDBで使用するものなので環境によって変わります
Discussion