英文:
Recommended approach to avoid duplicating username in MongoDB
问题
当一个新用户尝试在我的应用程序上注册时,验证过程的一部分是检查数据库(MongoDB)中的用户名和电子邮件是否已经被使用。这个验证过程是我的应用程序逻辑的一部分。
我想知道是否可以/更好地尝试保存用户而不进行这些验证检查,并让数据库检测到错误(由于重复的电子邮件地址或用户名),然后我可以处理这些错误。
如果可以的话,我该如何处理这些错误,以便我可以处理每个错误(用户名和电子邮件)。类似于以下方式...
err := c.Database.C("users").Insert(&user);
if err != nil {
//检查问题是与电子邮件还是用户名或两者都有关
}
英文:
When a new user attempts to register on my application at present, part of the validation process is to check against the database (MongoDB) whether the username and email are already in use. This validation process is part of my application logic.
I would like to know whether it's possible/better to attempt to save the user without these validation checks and let the DB detect an error (due to duplicated email address or username) which I could then handle.
If so, how could I handle these errors so that I could handle each (username and email). Something in the fashion of...
err := c.Database.C("users").Insert(&user);
if err != nil {
//check whether issue is with email or username or both
}
答案1
得分: 1
猜测唯一索引对您应该有效(如果upinsert不符合需求)。
在mongo命令中设置这些索引可以正常工作,另一种方法是在您的代码中使用mgo.EnsureIndex,可以在https://gist.github.com/border/3489566找到一个快速示例。
根据官方文档,一旦成功创建索引,除非索引被删除,否则EnsureIndex不会与服务器联系。
>一旦EnsureIndex成功返回,对于相同的索引的后续请求将不会与服务器联系,除非使用Collection.DropIndex删除相同的索引,或者调用Session.ResetIndexCache。
英文:
Guess the unique index should work for you (if the upinsert does not fit the need).
Setting those indices in mongo command works fine, another way is to use mgo.EnsureIndex in your code, a quick example can be found at https://gist.github.com/border/3489566.
According to the official document, once the index is successfully created, the EnsureIndex won't be contacting server unless the indices were dropped.
>Once EnsureIndex returns successfully, following requests for the same >index will not contact the server unless Collection.DropIndex is used to >drop the same index, or Session.ResetIndexCache is called.
答案2
得分: 0
你可以通过使用唯一索引来避免重复。在mongo shell中创建这样的索引,可以使用以下命令:
db.collection.createIndex( { name: 1 }, { unique: true } );
db.collection.createIndex( { email: 1 }, { unique: true } );
当你插入一个违反唯一性约束的文档时,你将收到一个错误结果。不幸的是,你将不得不解析错误消息的文本,以找出哪个索引被违反了。err.errmsg
将是一个以以下格式的字符串:
insertDocument :: caused by :: 11000 E11000 duplicate key error index: [index] dup key: [value]"
请注意唯一索引的一个奇怪特性。唯一索引将“字段不存在”视为一个单独的唯一值。因此,它允许集合中只有一个缺少该字段的文档。所以在将文档发送到数据库之前,请确保填写了所有必填字段。
英文:
You can avoid duplicates with an unique index. To create such an index in the mongo shell, use:
db.collection.createIndex( { name: 1 }, { unique: true } );
db.collection.createIndex( { email: 1 }, { unique: true } );
When you then insert a document which violates the uniqueness constraint, you will receive an error result. Unfortunately you will have to parse the text of the error message to find out which one of your indexes was violated. err.errmsg
will be a string in this format:
insertDocument :: caused by :: 11000 E11000 duplicate key error index: [index] dup key: [value]"
Be aware of a strange quirk of unique indexes. Unique indexes consider "field doesn't exist" as a separate unique value. So it allows exactly one document in the collection where the field is missing. So make sure that all required fields are filled before sending the document to the database.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论