我想打电话ensureIndex
on the authorName
,命令是什么以及我应该将其放在这段代码中的什么位置?
var mongoose = require('mongoose');
// defines the database schema for this object
var schema = mongoose.Schema({
projectName : String,
authorName : String,
comment : [{
id : String,
authorName : String,
authorEmailAddress : { type : String, index : true }
}]
});
// Sets the schema for model
var ProjectModel = mongoose.model('Project', schema);
// Create a project
exports.create = function (projectJSON) {
var project = new ProjectModel({
projectName : projectJSON.projectName,
authorName : projectJSON.authorName,
comment : [{
id : projectJSON.comments.id,
authorName : projectJSON.comments.authorName,
authorEmailAddress : projectJSON.authorEmailAddress
});
project.save(function(err) {
if (err) {
console.log(err);
} else{
console.log("success");
}
});
});
}
你不打电话ensureIndex
直接表明该字段应该在您的模式中建立索引,如下所示:
var schema = mongoose.Schema({
projectName : String,
authorName : { type: String, index: true }
});
根据该定义,Mongoose 将调用ensureIndex
当您通过以下方式注册模型时为您服务mongoose.model
call.
为了看到ensureIndex
Mongoose 正在进行的调用,通过将以下内容添加到代码中来启用调试输出:
mongoose.set('debug', true);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)