且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

确保索引不起作用 - MongoDB

更新时间:2023-01-30 10:10:03

没有问题,你可以看到索引在那里。你期望它不允许重复元素?为此,您必须将唯一标志设置为true:

There are no problems, you can see that the index is there. You're expecting it not to allow duplicate elements? For that you have to set unique flag to true:

db.users.ensureIndex({ name : 1},{unique: true});

更新:使用 {unique:true}再次运行确保不起作用,你必须再次放弃 ensureIndex

UPDATE: running ensure once again with {unique: true} doesn't work, you have to drop and ensureIndex again:

db.users.dropIndex({name:1})
db.users.ensureIndex({ name : 1}, {unique:true, dropDups : true});