在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中高效地插入文檔。