如何使用唯一索引字段正确更新ArangoDB文档?
我有以下users表的架构:如何使用唯一索引字段正确更新ArangoDB文档?,arangodb,unique-constraint,unique-index,Arangodb,Unique Constraint,Unique Index,我有以下users表的架构: name: string emails: [string] // unique index 索引的创建方式如下(使用go语言): 假设数据库中有以下用户: {_key: "1", name: "A", emails: ["aa@aa.aa"]} 在arangosh中使用以下命令添加电子邮件会返回错误: > db.users.update("1", {emails: ["aa@aa.aa", "aa2@aa.aa"]}) JavaScript e
name: string
emails: [string] // unique index
索引的创建方式如下(使用go
语言):
假设数据库中有以下用户:
{_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
返回类似错误
如何向给定用户正确添加电子邮件?我正在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._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"
]
}
]
我在阿兰戈什演示这个。创建集合:
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._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中的一个错误,稍后将解决:
显然,这是ArangoDB中的一个错误,稍后将解决:
好的,但这是AQL的问题。是否可以使用集合API更新文档?例如使用
.update
或.replace
功能?我做不到。它应该也可以与.replace()
一起使用,但是在3.4.x中引入了一个bug,如果您尝试,它会导致错误。此GitHub问题将跟踪此问题:作为一种解决方法,请将上述解决方案与AQL查询一起使用。好的,但这是与AQL一起使用的。是否可以使用集合API更新文档?例如使用.update
或.replace
功能?我做不到。它应该也可以与.replace()
一起使用,但是在3.4.x中引入了一个bug,如果您尝试,它会导致错误。这个问题将在GitHub问题中得到跟踪:作为一个解决方案,请使用上述解决方案和AQL查询。