【问题标题】:How to avoid duplicates in arangoDB如何避免arangoDB中的重复
【发布时间】:2016-04-07 09:59:48
【问题描述】:

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

【问题讨论】:

    标签: duplicates arangodb nosql


    【解决方案1】:

    执行此操作的 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
    

    【讨论】:

    • 我不明白你所说的“它不能在整个文档上创建,只能在其中的一部分上创建”。我为我的文档将包含的所有属性创建了一个 uniq 哈希索引。因此,当尝试上传与已在数据库中的一个文档具有相同索引属性值的文档时,我将收到您显示的错误。所以是的,我认为哈希索引是避免重复的最简单方法,尽管它也很容易避免。
    • 完整文档我的意思是{_id: '...', _key: '...', foo: true, bar: {...}} - 您将无法将索引放在顶级文档上,但即bar 中的所有内容或(作为附加参数监控)也@ 987654325@
    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-07
    • 1970-01-01
    • 2016-11-17
    • 2021-08-17
    相关资源
    最近更新 更多