猫鼬模式:'独特'不被尊重

问题描述:

我正试图在这种模式下使用猫鼬

I'm trying to use Mongoose with this schema

var RestoSchema = new Schema({
    "qname"          : {type: String, required: true, unique: true},
    ...
});

问题在于,这仍然允许使用现有的qname创建数据库的新条目.从我下面可以看到,索引已创建,但是当我使用.save方法时没有任何明显的影响.我有什么误会?

The problem is that this still permits new entries to the database to be created with an existing qname. From what i can see below the index has been created, but without any demonstrable impact when I use the .save method. What am I misunderstanding?

> db.restos.getIndexes()
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "ns" : "af.restos",
        "name" : "_id_"
    },
    {
        "v" : 1,
        "key" : {
            "qname" : 1
        },
        "ns" : "af.restos",
        "name" : "qname_1",
        "background" : true,
        "safe" : null
    }
]

getIndexes输出显示未将qname上的索引创建为唯一索引. Mongoose不会更改现有索引,因此您必须手动删除索引,然后重新启动应用程序,以便Mongoose可以将其重新创建为唯一索引.

The getIndexes output shows that the index on qname wasn't created as a unique index. Mongoose doesn't alter an existing index, so you'll have to manually drop the index and then restart your app so that Mongoose can re-create it as unique.

在外壳中:

db.restos.dropIndex('qname_1')