如何避免arangoDB中的重复

时间:2016-04-07 09:59:48

标签: duplicates arangodb nosql

在使用arangoDB时,是否有任何“简单”方法可以避免上传重复文档(使用自动生成的_key)?

1 个答案:

答案 0 :(得分:3)

Arango的做法是to use a uniq hash index。但是,无法在整个文档中创建它,只能创建它的一部分:

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