我正在使用mongoDB shell with version 3.0.2 我试图确保对集合users中的username字段的唯一性约束.
I am using mongoDB shell with version 3.0.2 I am trying to ensure uniqueness constraint over a username field in collection users.
这是我给的:
db.users.ensureIndex({"username": 1},{unique: true})它给了我以下错误:
{ "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "errmsg" : "exception: E11000 duplicate key error index: mybackend.users.$username_1 dup key: { : \"rahat\" }", "code" : 11000, "ok" : 0 }然后我在命令中使用dropDups: true:
db.users.ensureIndex({"username": 1},{unique: true, dropDups: true})然后我也遇到相同的错误:
Then too I get the same error:
{ "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "errmsg" : "exception: E11000 duplicate key error index: mybackend.users.$username_1 dup key: { : \"rahat\" }", "code" : 11000, "ok" : 0 }我还看到了此SO链接,但是在这里它已经有一个索引.我的没有.
I also saw this SO link but here it already had one index. Mine does not have one.
db.users.getIndexes()->
[ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "mybackend.users" } ]我在在github上找到此问题,并重新启动了mongo,但没有用.我究竟做错了什么?我想我在犯一个愚蠢的错误.请帮忙.
I looked for this issue on github and restarted the mongo but to no use. What am I doing wrong? I think I am doing a silly mistake. Please help.
推荐答案自Mongo 3.0版以来,不再支持在索引创建时使用drop drop功能.有关3.0的信息,请参见兼容性更改页面释放.
The drop duplicates functionality on index creation is no longer supported since Mongo version 3.0. See the compatibility changes page for the 3.0 release.
更多推荐
dropDups true不起作用mongodb
发布评论