Dovi
Dovi

Reputation: 853

How to avoid duplicates in arangoDB

Is there any "simple" way to avoid uploading a duplicate document (with auto-generated _key) while using arangoDB?

Upvotes: 1

Views: 1413

Answers (1)

dothebart
dothebart

Reputation: 6067

The Arango way of doing this is to use a uniq hash index. However, it can't be created over the full document, only parts of it:

db._create("testuniq")
[ArangoCollection 25260888467, "testuniq" (type document, status loaded)]
arangosh [_system]> db.testuniq.ensureIndex({ type: "hash", fields:["uniqDocument", "uniqAttribute"], unique: true })
{ 
  "id" : "testuniq/25264886163", 
  "type" : "hash", 
  "fields" : [ 
    "uniqDocument", 
    "uniqAttribute" 
  ], 
  "selectivityEstimate" : 1, 
  "unique" : true, 
  "sparse" : false, 
  "isNewlyCreated" : true, 
  "code" : 201 
}
arangosh [_system]> db.testuniq.save({uniqDocument: {foo: "bar"}})
arangosh [_system]> db.testuniq.save({uniqDocument: {foo: "bar"}})
JavaScript exception in file 'js/client/modules/org/arangodb/arangosh.js' at 106,13: ArangoError 1210: cannot create document, unique constraint violated
!      throw error;
!            ^
stacktrace: ArangoError: cannot create document, unique constraint violated
    at Object.exports.checkRequestResult (js/client/modules/org/arangodb/arangosh.js:104:21)
    at ArangoCollection.save.ArangoCollection.insert (js/client/modules/org/arangodb/arango-collection.js:1014:12)
    at <shell command>:1:13
arangosh [_system]> db.testuniq.save({uniqAttribute: "bar"})
arangosh [_system]> db.testuniq.save({uniqAttribute: "bar"})
JavaScript exception in file 'js/client/modules/org/arangodb/arangosh.js' at 106,13: ArangoError 1210: cannot create document, unique constraint violated
!      throw error;
!            ^
stacktrace: ArangoError: cannot create document, unique constraint violated
    at Object.exports.checkRequestResult (js/client/modules/org/arangodb/arangosh.js:104:21)
    at ArangoCollection.save.ArangoCollection.insert (js/client/modules/org/arangodb/arango-collection.js:1014:12)
    at <shell command>:1:13

Upvotes: 3

Related Questions