我在用mongoDB shell with version 3.0.2
我试图确保唯一性约束username
集合中的字段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})
然后我也得到同样的错误:
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"errmsg" : "exception: E11000 duplicate key error index: mybackend.users.$username_1 dup key: { : \"rahat\" }",
"code" : 11000,
"ok" : 0
}
我也看到了这个链接但这里它已经有一个索引了。我的没有。
db.users.getIndexes()
->
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "mybackend.users"
}
]
我寻找这个问题在github上并重新启动 mongo 但没有用。我究竟做错了什么?我想我犯了一个愚蠢的错误。请帮忙。