簡體   English   中英

如何正確地使用唯一索引字段更新ArangoDB文檔?

[英]How to properly update an ArangoDB document with unique index field?

我為用戶表具有以下架構:

   name: string
   emails: [string] // unique index

索引以以下方式(使用go語言)創建:

usersCollection.EnsureHashIndex(
    ctx, 
    []string{"emails[*]"}, 
    driver.EnsureHashIndexOptions{Unique: true, Sparse: true})

假設我在數據庫中有以下用戶:

{_key: "1", name: "A", emails: ["aa@aa.aa"]}

arangosh使用以下命令添加電子郵件arangosh返回錯誤:

> db.users.update("1", {emails: ["aa@aa.aa", "aa2@aa.aa"]})
JavaScript exception in file '/usr/share/arangodb3/js/client/modules/@arangodb/arangosh.js' at 99,7: ArangoError 1210: unique constraint violated - in index 442818 of type rocksdb-hash over ["emails[*]"]; conflicting key: 1

使用replace返回類似的錯誤。

如何正確向給定用戶添加電子郵件?

我正在arangosh演示這個。 創建集合:

db._create('usersCollection')
[ArangoCollection 497, "usersCollection" (type document, status loaded)]

將唯一索引添加到集合中:


db.usersCollection.ensureIndex({ type: "hash", fields: [ "emails[*]" ], unique: true, sparse: true })
{ 
  "deduplicate" : true, 
  "fields" : [ 
    "emails[*]" 
  ], 
  "id" : "usersCollection/517", 
  "isNewlyCreated" : true, 
  "selectivityEstimate" : 1, 
  "sparse" : true, 
  "type" : "hash", 
  "unique" : true, 
  "code" : 201 
}

像您一樣創建條目:

 db.usersCollection.save({_key: "1", name: "A", emails: ["aa@aa.aa"]})
{ 
  "_id" : "usersCollection/1", 
  "_key" : "1", 
  "_rev" : "_YSk15je---" 
}

現在,我們使用AQL更新電子郵件字段以添加其他電子郵件:

db._query(`UPDATE '1' WITH {emails: ["aa@aa.aa", "bb@bb.bb"]} IN usersCollection`)
[object ArangoQueryCursor, count: 0, cached: false, hasMore: false]

檢查回復:

db.usersCollection.toArray()
[ 
  { 
    "_key" : "1", 
    "_id" : "usersCollection/1", 
    "_rev" : "_YSk2J1K--_", 
    "name" : "A", 
    "emails" : [ 
      "aa@aa.aa", 
      "bb@bb.bb" 
    ] 
  } 
]

顯然,這是ArangoDB中的錯誤,稍后會解決:

https://github.com/arangodb/arangodb/issues/8359

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM