英文:
Parsing error in mongodb db, insert to collection with unique index
问题
我在mongodb中有一个集合,其中包含以下形式的文档:
{
"user": "user1",
"email": "user1@example.com",
}
其中字段"user"和"email"是唯一的。我想要向集合中插入一个新用户,并检查这两个值的唯一性。我可以使用mgo在golang中进行插入操作,代码如下:
session.SetSafe(&mgo.Safe{}) // 确保mgo等待错误
user := struct{
string `bson:"user"`
string `bson:"email"`
}{
"user1",
"user1@different.com"
}
err := users.Insert(user) // 这里的users是*mgo.Collection类型
如果我打印err
,它会输出insertDocument :: caused by :: 11000 E11000 duplicate key error index: kails.users.$name_1 dup key: { : "user1" }
有没有一种惯用的方法可以使用这个错误来找出哪个值不是唯一的?如果不是两个值都不唯一,是否还有其他步骤需要执行?使用正则表达式解析字符串感觉...不太对。
如果不能使用错误来确定哪个值不唯一,是否有其他替代方案来进行"$or"查询(检查唯一性)+插入操作?
我已经阅读了mgo文档,希望没有漏掉重要的内容。
英文:
I have a collection in mongodb with documents of the form:
{
"user": "user1",
"email: "user1@example.com",
}
Where the fields "user" and "email" are unique. I want to insert a new user to the collection, while checking for uniqueness of both values. I can do an insert in golang with mgo like this:
session.SetSafe(&mgo.Safe{}) // ensure mgo waits for errors
user := struct{
string `bson:"user"`
string `bson:"email"`
}{
"user1",
"user1@different.com"
}
err := users.Insert(user) // where user is type *mgo.Collection
If I print err
it outputs insertDocument :: caused by :: 11000 E11000 duplicate key error index: kails.users.$name_1 dup key: { : "user1" }
Is there an idiomatic way to use this error to find which of the values wasn't unique?, if not both? (or are there other steps needed?). Parsing the string using a regexp feels... wrong.
If it's not possible to use the error to find if not unique, are there any alternatives to an "$or" query (check for unique) + insert?
I've read the mgo documentation, hope I didn't miss anything important.
答案1
得分: 10
func IsDup(err error) bool
IsDup
函数返回一个布尔值,指示错误err
是否表示由于主键索引或唯一索引已经存在具有给定值的条目而导致的重复键错误。
例如:
err := users.Insert(user) // 其中user的类型为*mgo.Collection
if err != nil {
if mgo.IsDup(err) {
// 是重复键错误,但我们不知道是哪一个
}
// 是其他错误
}
不幸的是,在可能存在多个唯一索引的情况下,似乎没有办法确定哪个值不唯一。
不过,你可以使用具有ID
和Email
而不是user
和email
的User
结构体。ID将由Mongo在插入时自动生成,并且Email将具有唯一索引。只要你不需要任何其他唯一索引,你可以安全地假设IsDup == true
表示只有重复的电子邮件地址。
电子邮件地址是很好的用户名,因为这样用户就少了一个要记住的东西
英文:
http://godoc.org/labix.org/v2/mgo#IsDup
> func IsDup(err error) bool
>
> IsDup returns whether err informs of a duplicate key error because a primary key index or a secondary unique index already has an entry with the given value.
e.g.
err := users.Insert(user) // where user is type *mgo.Collection
if err != nil {
if mgo.IsDup(err) {
// Is a duplicate key, but we don't know which one
}
// Is another error
}
Unfortunately there does not appear to be a way to discern which value is not unique in the case where you might have several unique indexes in place.
You could instead have a User struct with ID
and Email
instead of user
and email
, though. ID would be auto-generated on insert by Mongo, and Email would have a unique index. Provided you didn't need any further unique indexes you could then safely assume that an IsDup == true
case means that there's a duplicate email address only.
Email addresses are good usernames as it's one less thing for users to remember
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论