如何检查集合是否已存在于 ArangoDB 中
How to check if a collection already exists in ArangoDB
假设我有一个集合 Col1
已经存在于我的数据库中。所以,做这样的事情:
var col = db.collection('Col1');
col.save({"name":"something"});
将完美运行。
但是如果我的数据库中不存在的集合 Col2
被尝试使用相同的东西,即
var col = db.collection('Col2');
col.save({"name":"something"})
也可以完美地工作。只是它不存在并且不会显示在我的数据库中。如果它抛出一些错误或东西,我可以使用 try
和 catch
语句作为结果。但既然那是现成的,我怎么知道一个集合是否已经存在?
https://docs.arangodb.com/3.1/Manual/DataModeling/Collections/DatabaseMethods.html#collection 状态
returns a single collection or null db._collection(collection-name)
所以你可以使用
var col2 = db._collection('Col2');
if (col2) {
// collection exists
col2.save({"name":"something"});
}
col.save
不会立即执行保存操作,而是 returns 一个承诺。所以总会成功的。解决方案是等待 promise 被解决,然后根据是否发生错误做出反应:
var col = db.collection('Col2');
col.save({"name":"something"}).then(
meta => console.log('Document saved:', meta._rev),
err => { console.error('Failed to save document:', err.errorNum, err.response.body.errorMessage); }
);
这里有两件事可能令人困惑。
首先,aragojs(不同于 ArangoDB 的内部 JS API)对于需要与实际 ArangoDB 服务器通信的所有内容都是异步。异步函数在文档中标记为 "async"。
你可以传递一个 node.js 风格的回调(就像内置 node.js 模块中的异步函数,例如 fs
、http
等)给这些方法。或者,您可以简单地省略回调,该方法将 return 对结果的承诺。您可以了解更多关于 promises 是如何工作的 in Mozilla's JavaScript reference documentation(这不是特定于 Mozilla —— 他们的参考非常好而且通常是正确的)。
您 运行 关注的另一件事是 arangojs 中的集合对象与 ArangoDB 中的实际集合之间的区别。该驱动程序允许您为集合创建集合对象,无论它们是否存在。如果集合实际上不存在,当尝试使用它们时,您当然会看到一个错误。
var col = db.collection('whatever');
col.create() // create the collection if it doesn't exist
.catch(function () {}) // ignore any errors
.then(function () {
return col.get(); // make sure the collection exists now
})
.then(function () {
return col.save({some: 'data'});
})
.then(function (result) {
// everything went fine
})
.catch(function (e) {
console.error('Something went wrong', e.stack);
});
或使用 async/await(如果您使用 Babel 或在一年后阅读此答案):
var col = db.collection('whatever');
try {
await col.create(); // create the collection if it doesn't exist
} catch (e) {} // ignore any errors
try {
await col.get(); // make sure the collection exists now
const result = await col.save({some: 'data'});
// everything went fine
} catch (e) {
console.error('Something went wrong', e.stack);
}
或者使用 node.js 风格的回调,因为你是老派或者真的很喜欢金字塔:
var col = db.collection('whatever');
col.create(function () { // create the collection if it doesn't exist
// ignore any errors
col.get(function (err) { // make sure the collection exists now
if (err) {
console.error('Something went wrong', err.stack);
return;
}
col.save({some: 'data'}, function (err, result) {
if (err) {
console.error('Something went wrong', err.stack);
return;
}
// everything went fine
});
});
});
这是旧的,但有一个 exists()
功能。
typescript/node
中的示例
const metaResult = db.collection('myCollection');
if(!await metaResult.exists()) {
await db.createCollection('myCollection');
}
假设我有一个集合 Col1
已经存在于我的数据库中。所以,做这样的事情:
var col = db.collection('Col1');
col.save({"name":"something"});
将完美运行。
但是如果我的数据库中不存在的集合 Col2
被尝试使用相同的东西,即
var col = db.collection('Col2');
col.save({"name":"something"})
也可以完美地工作。只是它不存在并且不会显示在我的数据库中。如果它抛出一些错误或东西,我可以使用 try
和 catch
语句作为结果。但既然那是现成的,我怎么知道一个集合是否已经存在?
https://docs.arangodb.com/3.1/Manual/DataModeling/Collections/DatabaseMethods.html#collection 状态
returns a single collection or null db._collection(collection-name)
所以你可以使用
var col2 = db._collection('Col2');
if (col2) {
// collection exists
col2.save({"name":"something"});
}
col.save
不会立即执行保存操作,而是 returns 一个承诺。所以总会成功的。解决方案是等待 promise 被解决,然后根据是否发生错误做出反应:
var col = db.collection('Col2');
col.save({"name":"something"}).then(
meta => console.log('Document saved:', meta._rev),
err => { console.error('Failed to save document:', err.errorNum, err.response.body.errorMessage); }
);
这里有两件事可能令人困惑。
首先,aragojs(不同于 ArangoDB 的内部 JS API)对于需要与实际 ArangoDB 服务器通信的所有内容都是异步。异步函数在文档中标记为 "async"。
你可以传递一个 node.js 风格的回调(就像内置 node.js 模块中的异步函数,例如 fs
、http
等)给这些方法。或者,您可以简单地省略回调,该方法将 return 对结果的承诺。您可以了解更多关于 promises 是如何工作的 in Mozilla's JavaScript reference documentation(这不是特定于 Mozilla —— 他们的参考非常好而且通常是正确的)。
您 运行 关注的另一件事是 arangojs 中的集合对象与 ArangoDB 中的实际集合之间的区别。该驱动程序允许您为集合创建集合对象,无论它们是否存在。如果集合实际上不存在,当尝试使用它们时,您当然会看到一个错误。
var col = db.collection('whatever');
col.create() // create the collection if it doesn't exist
.catch(function () {}) // ignore any errors
.then(function () {
return col.get(); // make sure the collection exists now
})
.then(function () {
return col.save({some: 'data'});
})
.then(function (result) {
// everything went fine
})
.catch(function (e) {
console.error('Something went wrong', e.stack);
});
或使用 async/await(如果您使用 Babel 或在一年后阅读此答案):
var col = db.collection('whatever');
try {
await col.create(); // create the collection if it doesn't exist
} catch (e) {} // ignore any errors
try {
await col.get(); // make sure the collection exists now
const result = await col.save({some: 'data'});
// everything went fine
} catch (e) {
console.error('Something went wrong', e.stack);
}
或者使用 node.js 风格的回调,因为你是老派或者真的很喜欢金字塔:
var col = db.collection('whatever');
col.create(function () { // create the collection if it doesn't exist
// ignore any errors
col.get(function (err) { // make sure the collection exists now
if (err) {
console.error('Something went wrong', err.stack);
return;
}
col.save({some: 'data'}, function (err, result) {
if (err) {
console.error('Something went wrong', err.stack);
return;
}
// everything went fine
});
});
});
这是旧的,但有一个 exists()
功能。
typescript/node
中的示例const metaResult = db.collection('myCollection');
if(!await metaResult.exists()) {
await db.createCollection('myCollection');
}