我对用户表具有以下架构:
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
中使用以下命令添加电子邮件会返回错误:
> 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
返回类似的错误。
如何正确向给定用户添加电子邮件?
答案 0 :(得分:2)
我正在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"
]
}
]
答案 1 :(得分:0)
显然,这是ArangoDB中的错误,稍后会解决: