0
votes

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")
    }
  }
1

1 Answers

1
votes

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"
  }
}