在ArangoDB中,为了高效地插入文档,您可以遵循以下建议:
db.collection.save()
方法一次插入多个文档。例如:const documents = [
{ name: "Alice", age: 30 },
{ name: "Bob", age: 25 },
{ name: "Carol", age: 35 }
];
db.users.save(documents);
db._executeTransaction()
方法。例如:const transaction = String(function (user1, user2) {
const { collections } = require("@arangodb");
const usersCollection = collections.users;
const ordersCollection = collections.orders;
// 插入用户文档
const user1Id = usersCollection.save({ name: user1.name, age: user1.age });
const user2Id = usersCollection.save({ name: user2.name, age: user2.age });
// 插入订单文档
ordersCollection.save({ userId: user1Id, product: user1.product });
ordersCollection.save({ userId: user2Id, product: user2.product });
});
db._executeTransaction(
{
write: [usersCollection.name, ordersCollection.name],
},
transaction,
{ user1: { name: "Alice", age: 30, product: "laptop" }, user2: { name: "Bob", age: 25, product: "phone" } }
);
const collection = db.collection("myCollection");
// 禁用索引
collection.dropIndexes();
// 插入文档
collection.save({ name: "John", age: 28 });
// 重新启用索引
collection.createIndex({ name: "name" });
const docId = "myDocumentId";
const doc = { name: "Jane", age: 29 };
db.collection("myCollection").save(doc, docId);
db._query()
方法并设置timeout
选项。例如:const result = db._query("FOR doc IN myCollection INSERT doc INTO myTargetCollection RETURN doc", {}, { timeout: 10000 }); // 设置超时时间为10秒
遵循这些建议,您应该能够在ArangoDB中高效地插入文档。