簡體   English   中英

如何檢查集合是否已存在於 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"})

也能正常工作。 只是它不存在並且不會顯示在我的數據庫中。 如果它拋出了一些錯誤或東西,我可以使用trycatch語句來獲取結果。 但既然那不在盤子里,我怎么知道一個集合是否已經存在?

這里有兩件事可能令人困惑。

首先,arangojs(與 ArangoDB 的內部 JS API 不同)對於需要與實際 ArangoDB 服務器通信的所有內容都是異步的。 異步函數在文檔中被標記為“async”。

您可以將 node.js 樣式的回調(如內置 node.js 模塊中的異步函數,例如fshttp等)傳遞給這些方法。 或者,您可以簡單地省略回調,該方法將返回結果的承諾。 您可以在 Mozilla 的 JavaScript 參考文檔中了解有關 Promise 如何工作的更多信息(這不是 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
    });
  });
});

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); }
);

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

返回單個集合或 null db._collection(collection-name)

所以你可以使用

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

這是舊的,但有一個exists()函數。

打字稿/節點中的示例

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM