【问题标题】:How to check if a collection already exists in ArangoDB如何检查 ArangoDB 中是否已存在集合
【发布时间】:2016-11-09 08:59:13
【问题描述】:

假设我的数据库中已经存在一个集合Col1。所以,做这样的事情:

var col = db.collection('Col1');
col.save({"name":"something"});

可以正常工作。

但是,如果我的数据库中尚不存在的集合Col2 尝试使用相同的东西,即

var col = db.collection('Col2');
col.save({"name":"something"})

也可以正常工作。只是它不存在并且不会显示在我的数据库中。如果它抛出了一些错误或东西,我可以使用trycatch 语句作为结果。但既然那是不合情理的,我怎么知道一个集合是否已经存在?

【问题讨论】:

    标签: node.js arangodb arangojs


    【解决方案1】:

    这里有两件事情可能会令人困惑。

    首先,arangojs(与 ArangoDB 的内部 JS API 不同)对于需要与实际 ArangoDB 服务器通信的所有内容都是异步的。异步函数在文档中被标记为“异步”。

    您可以将 node.js 样式的回调(如内置 node.js 模块中的异步函数,例如 fshttp 等)传递给这些方法。或者,您可以简单地省略回调,该方法将返回结果的承诺。您可以了解有关 Promise 工作原理的更多信息 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
        });
      });
    });
    

    【讨论】:

      【解决方案2】:

      col.save 不会立即执行保存操作,而是返回一个承诺。所以总会成功的。解决方案是等待 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); }
      );
      

      【讨论】:

      • 逻辑看起来不错,但是这段代码也返回了Promise { <pending> }。知道为什么吗?
      【解决方案3】:

      https://docs.arangodb.com/3.1/Manual/DataModeling/Collections/DatabaseMethods.html#collection 状态

      返回单个集合或 null db._collection(collection-name)

      所以你可以使用

      var col2 = db._collection('Col2');
      if (col2) {
          // collection exists
          col2.save({"name":"something"});
      }
      

      【讨论】:

      • 虽然 arangoDB 的文档说它可以工作,但节点的包 arangojs 并不同意。对于不存在的集合,我的代码在 if 子句中显示true。我想要一个 arangojs 解决方案。目前我只是在所有集合列表中检查集合名称以检查集合的存在。但是我的方法很粗糙,我自己也不喜欢
      【解决方案4】:

      这是旧的,但有一个exists() 函数。

      打字稿/节点中的示例

      const metaResult = db.collection('myCollection');
      if(!await metaResult.exists()) {
          await db.createCollection('myCollection');
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-17
        • 1970-01-01
        • 2019-01-21
        • 2018-02-27
        • 2023-03-13
        • 2018-08-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多