I have the following schema for users table:
name: string
emails: [string] // unique index
The index is created in the following way (in go
language):
usersCollection.EnsureHashIndex(
ctx,
[]string{"emails[*]"},
driver.EnsureHashIndexOptions{Unique: true, Sparse: true})
Let's say I have the following user in the DB:
{_key: "1", name: "A", emails: ["aa@aa.aa"]}
Adding an email using the following command in arangosh
returns an error:
> 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
Using replace
returns similar error.
How can I properly add an email to the given user?
I'm demonstrating this in arangosh. Creating the collection:
db._create('usersCollection')
[ArangoCollection 497, "usersCollection" (type document, status loaded)]
Add the unique index to the collection:
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
}
Create the entry as you did:
db.usersCollection.save({_key: "1", name: "A", emails: ["aa@aa.aa"]})
{
"_id" : "usersCollection/1",
"_key" : "1",
"_rev" : "_YSk15je---"
}
Now we use AQL to update the emails field to add the other email:
db._query(`UPDATE '1' WITH {emails: ["aa@aa.aa", "bb@bb.bb"]} IN usersCollection`)
[object ArangoQueryCursor, count: 0, cached: false, hasMore: false]
Inspecting the reply:
db.usersCollection.toArray()
[
{
"_key" : "1",
"_id" : "usersCollection/1",
"_rev" : "_YSk2J1K--_",
"name" : "A",
"emails" : [
"aa@aa.aa",
"bb@bb.bb"
]
}
]
Apparently this is a bug in ArangoDB and will be solved later on:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.