如何正确更新具有唯一索引字段的 ArangoDB 文档?
How to properly update an ArangoDB document with unique index field?
我有以下用户架构 table:
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
returns 中使用以下命令添加电子邮件出现错误:
> 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
returns类似错误。
如何正确地向给定用户添加电子邮件?
我在 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 中的一个错误,稍后将解决:
我有以下用户架构 table:
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
returns 中使用以下命令添加电子邮件出现错误:
> 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
returns类似错误。
如何正确地向给定用户添加电子邮件?
我在 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 中的一个错误,稍后将解决: