Node.js 为什么在添加唯一索引时必须使用schema.path?
如果索引已在第一行中声明,为什么我们必须添加第二行Node.js 为什么在添加唯一索引时必须使用schema.path?,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,如果索引已在第一行中声明,为什么我们必须添加第二行 var schema = new Schema({ name: { type: String, unique: true }}); >> schema.path('name').index({ unique: true }); 我想使用较新的“createIndexes”函数,而不是较旧的(而且性能可能较低)、不推荐使用的“ensureIndex”函数: 使用{unique:true},您将得到以下错误消息: (节点:2924)弃
var schema = new Schema({ name: { type: String, unique: true }});
>> schema.path('name').index({ unique: true });
我想使用较新的“createIndexes”函数,而不是较旧的(而且性能可能较低)、不推荐使用的“ensureIndex”函数: 使用
{unique:true}
,您将得到以下错误消息:
(节点:2924)弃用警告:collection.ensureIndex已弃用。
改用createindex
如果使用显式的索引声明,不仅代码更干净、更易于阅读,而且警告也会消失。如果使用显式声明,还可以省略{unique:true}语句,因为它可能会被忽略。我使用
“useCreateIndex”:true
来隐藏不推荐。Soschema.path('name').index({unique:true})这是一个明确的声明?是的,我相信这应该是相同的!