1

The documentation is located here:
Document Manipulation · ArangoDB v3.4.1 Drivers Documentation

I see the documentation for collection.replace() and collection.update(), but nothing for collection.save(). I know the save function exits because I'm using it. But it doesn't return the expected value and I'd like to reference the documentation.

My specific problem is that I want to save a document to the ArangoDB database and get back the saved document in full. Here's what I have so far:

  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")
    }
  }
BugBuddy
  • 576
  • 2
  • 5
  • 19

1 Answers1

2

The documentation of the save method is found under DocumentCollection:

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

The answer you look for:

returns an object containing the document's metadata

I admit this isn't very detailed. What it returns are the system attributes _id, _key and _rev. This also applies if you save an edge with a _from and a _to attribute, they are not returned as meta data, nor any user attributes even if their names start with an underscore.

If you want it to return the full document, then set the option returnNew:

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

If set to true, return additionally the complete new documents under the attribute new in the result.

The result looks like this:

{
  "_id": "coll/123",
  "_key": "123",
  "_rev": "_YDWaEaa--B",
  "new": {
    "_id": "coll/123",
    "_key": "123",
    "_rev": "_YDWaEaa--B",
    "foo": "bar"
  }
}
CodeManX
  • 11,159
  • 5
  • 49
  • 70
  • Thank you. I'm not sure how I ended up on the v 3.4.1 documentation, but it is interesting that the collection.save() method is not part of 3.4.1 docs. Anyway, your explanation is far better than the docs. – BugBuddy Jan 16 '19 at 23:20
  • The link you posted points to the same documentation version, but a different page (_Document Manipulation_ as opposed to _DocumentCollection_). I think the reason why the docs are slightly confusing is that they seem to follow the way it is implemented. Because of the "inheritance" you find part of the methods described in one page, but others on another. From a user stand point, this makes it rather complicated to find the right page. – CodeManX Jan 17 '19 at 10:38