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

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

问题描述

我试图让 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/问题/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.

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

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