I would like to call ensureIndex on the authorName, what is the command and where in this code should I put it?
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");
}
});
});
}
You don’t call
ensureIndexdirectly, you indicate that field should be indexed in your schema like this:Based on that definition, Mongoose will call
ensureIndexfor you when you register the model via themongoose.modelcall.To see the
ensureIndexcalls that Mongoose is making, enable debug output by adding the following to your code: