猫鼬唯一索引不起作用! [英] Mongoose Unique index not working!

查看:55
本文介绍了猫鼬唯一索引不起作用!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让MongoDB根据其索引检测重复值.我认为这在MongoDB中是可能的,但是通过Mongoose包装器,事情似乎被打破了.所以对于这样的事情:

I'm trying to let MongoDB detect a duplicate value based on its index. I think this is possible in MongoDB, but through the Mongoose wrapper things appear to be broken. So for something like this:

User = new Schema ({
  email: {type: String, index: {unique: true, dropDups: true}}
})

我可以用同一封电子邮件保存2个用户.真该死!

I can save 2 users with the same email. Darn.

在这里也表达了同样的问题: https://github.com/LearnBoost/mongoose/issues/56 ,但是该线程很旧,无法通向任何地方.

The same issue has been expressed here: https://github.com/LearnBoost/mongoose/issues/56, but that thread is old and lead to nowhere.

现在,我正在手动调用数据库以查找用户.由于对电子邮件"进行了索引,因此该调用并不昂贵.但是让它在本地处理仍然会很好.

For now, I'm manually making a call to the db to find the user. That call is not expensive since "email" is indexed. But it would still be nice to let it be handled natively.

有人对此有解决方案吗?

Does anyone have a solution to this?

推荐答案

糟糕!您只需要重新启动mongo.

Oops! You just have to restart mongo.

这篇关于猫鼬唯一索引不起作用!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆