如果您需要批量添加边缘,则应优先使用ArangoDB的bulk API。该API非常适合处理大量数据,并且可以显着提高性能。
下面是一个示例代码段,说明如何使用ArangoDB的bulk API批量添加边缘:
const arangojs = require("arangojs");
const db = new arangojs.Database();
async function addEdges(edges) {
const edgeCollection = db.collection("myEdges");
const results = await edgeCollection.import(edges, { type: "auto" });
return results;
}
const edges = [
{ _from: "myVertices/123", _to: "myVertices/456", type: "edgeType1" },
{ _from: "myVertices/456", _to: "myVertices/789", type: "edgeType2" },
// Add more edges here...
]
addEdges(edges)
.then(results => {
console.log(`Added ${results.created} edges`);
})
.catch(err => {
console.error(err);
});
在此示例中,我们使用arangojs
驱动程序来连接到ArangoDB。我们定义了一个名为addEdges
的异步函数,该函数使用bulk API批量导入边缘。edges
数组包含待添加的边缘。
使用import
方法时,我们必须指定导入的数据类型,可以通过type
选项指定。在此示例中,我们使用"auto"
作为类型,表示ArangoDB将自动检测数据类型。如果您知道导入的数据是何种类型,则应指定该类型,以提高性能。
在添加边缘时,我们可以在每个元素中使用_from
和_to
属性指定开头和结尾顶点的ID。我们还可以定义其他属性,例如type
。
最后,我们使用.then()
和.catch()
方法处理Promise。在这种情况下,我们