arangodb / arangojs

The official ArangoDB JavaScript driver.
https://arangodb.github.io/arangojs
Apache License 2.0
604 stars 107 forks source link

onDuplicate not working #502

Closed ralfr closed 6 years ago

ralfr commented 6 years ago

Using arangojs ^6.0.1 with latest node and arangodb.

When trying to save (updates to) an existing document via:

collection.save(doc, {onDuplicate: 'replace'})

it fails with

errorMessage: 'unique constraint violated - in index 0 of type primary over ["_key"]; conflicting key:

My expectation would be for arangodb to gracefully replace the document as per the documentation.

jsteemann commented 6 years ago

I don't think that collection.save has an onDuplicate parameter that would do that. May I ask where you got the info from? I can the task is to replace an existing document, there is also collection.replace, in case it should be just partially updated, there is collection.update.

ralfr commented 6 years ago

Thanks for responding so fast - awesome support! I got it from here.

I essentially want an UPSERT operation from a node application. Will collection.replace do just that?

jsteemann commented 6 years ago

Ah, got it. I think that question referred to collection.import, which does a bulk insert/replacement of documents (in contrast to collection.save and collection.replace which are single-document operations). As there is no first-class collection.upsert operation in arangojs yet, there are the following three options for performing it:

pluma commented 6 years ago

I'm closing this issue as it looks like the question has been answered and there hasn't be a follow-up question in two months. If you have more questions about ArangoDB I recommend checking out our community slack at https://slack.arangodb.com/

Simran-B commented 6 years ago

@jsteemann Using the following query pattern, can many documents (10k+) be sent at once, or should one send batches (e.g. 1k at a time)?

FOR doc IN @docs
  UPSERT { _key: doc._key }
  INSERT doc
  UPDATE doc
{
  "docs": [
    {"_key": "upsert_test", "foo": "bar"},
    {"no": "key"}
  ]
}

BTW: This pattern works really nicely. If no key is provided for a document, a new document will be inserted with an auto-generated key. This is also the case if documents exists with key "null" or "undefined".