下面是一个使用ArangoDB的代码示例,演示了如何使用文档数组和键/值集合。
// 引入 ArangoDB 驱动程序
const arangojs = require("arangojs");
const db = new arangojs.Database();
// 连接到数据库
db.useBasicAuth("username", "password");
db.useDatabase("myDatabase");
db.useBearerAuth("token");
// 集合名称
const collectionName = "myCollection";
// 创建文档数组
const documents = [
{
_key: "document1",
name: "John Doe",
age: 30
},
{
_key: "document2",
name: "Jane Smith",
age: 25
},
{
_key: "document3",
name: "Bob Johnson",
age: 35
}
];
// 将文档数组插入到集合中
async function insertDocuments() {
const collection = db.collection(collectionName);
const result = await collection.import(documents);
console.log(result);
}
// 创建键/值集合
async function createKeyValueCollection() {
const collection = db.collection(collectionName);
await collection.create(type: "document");
console.log("Key/value collection created");
}
// 将键值对插入到集合中
async function insertKeyValue() {
const collection = db.collection(collectionName);
const key = "myKey";
const value = "myValue";
await collection.save({ _key: key, value: value });
console.log("Key/value pair inserted");
}
// 查询集合中的文档数组
async function queryDocuments() {
const collection = db.collection(collectionName);
const cursor = await collection.all();
const documents = await cursor.all();
console.log(documents);
}
// 查询集合中的键值对
async function queryKeyValue() {
const collection = db.collection(collectionName);
const document = await collection.document("myKey");
console.log(document);
}
// 执行示例函数
async function runExample() {
await insertDocuments();
await createKeyValueCollection();
await insertKeyValue();
await queryDocuments();
await queryKeyValue();
}
runExample();
上面的代码示例包含了以下操作:
请确保在代码中替换username
、password
、myDatabase
和token
为您自己的ArangoDB数据库的凭据和数据库名称。同时,您还可以根据需要修改集合名称和文档数组/键值对的数据。