【问题标题】:what is the return value of ArangoJS collection.save()?ArangoJS collection.save() 的返回值是多少?
【发布时间】:2019-01-16 05:37:04
【问题描述】:

文档位于:
Document Manipulation · ArangoDB v3.4.1 Drivers Documentation

我看到了 collection.replace()collection.update() 的文档,但没有看到 collection.save() 的文档。我知道保存功能退出,因为我正在使用它。但它没有返回预期值,我想参考文档。

我的具体问题是我想将一个文档保存到 ArangoDB 数据库并完整地取回保存的文档。到目前为止,这是我所拥有的:

  async createDocument(collectionName, data) {
    try {
      const collection = this.db.collection(collectionName);
      return collection.save(data); //I want to return the saved document
    } catch(err) {
      console.log(err.message, "saving failed")
    }
  }

【问题讨论】:

    标签: arangodb arangojs


    【解决方案1】:

    保存方法的文档在DocumentCollection下:

    https://docs.arangodb.com/3.4/Drivers/JS/Reference/Collection/DocumentCollection.html#documentcollectionsave

    您要寻找的答案:

    返回一个包含文档元数据的对象

    我承认这不是很详细。它返回的是系统属性_id_key_rev。如果您使用_from_to 属性保存边缘,这也适用,它们不会作为元数据返回,也不会作为任何用户属性返回,即使它们的名称以下划线开头。

    如果你希望它返回完整的文档,那么设置选项returnNew:

    collection.save(data, { returnNew: true} );
    

    如果设置为 true,则在结果中额外返回属性 new 下的完整新文档。

    结果如下:

    {
      "_id": "coll/123",
      "_key": "123",
      "_rev": "_YDWaEaa--B",
      "new": {
        "_id": "coll/123",
        "_key": "123",
        "_rev": "_YDWaEaa--B",
        "foo": "bar"
      }
    }
    

    【讨论】:

    • 谢谢。我不确定我是如何结束 v 3.4.1 文档的,但有趣的是 collection.save() 方法不是 3.4.1 文档的一部分。无论如何,您的解释比文档要好得多。
    • 您发布的链接指向相同的文档版本,但页面不同(Document ManipulationDocumentCollection 相对)。我认为这些文档有点令人困惑的原因是它们似乎遵循了它的实现方式。由于“继承”,您可以在一页中找到部分方法,而在另一页中找到其他方法。从用户的角度来看,这使得找到正确的页面变得相当复杂。
    猜你喜欢
    • 1970-01-01
    • 2017-01-06
    • 2010-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2020-12-13
    相关资源
    最近更新 更多