Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
76
rated 0 times [  78] [ 2]  / answers: 1 / hits: 24286  / 12 Years ago, wed, november 7, 2012, 12:00:00

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);
}
});
});
}

More From » mongodb

 Answers
12

You don't call ensureIndex directly, you indicate that field should be indexed in your schema like this:



var schema = mongoose.Schema({
projectName : String,
authorName : { type: String, index: true }
});


Based on that definition, Mongoose will call ensureIndex for you when you register the model via the mongoose.model call.



To see the ensureIndex calls that Mongoose is making, enable debug output by adding the following to your code:



mongoose.set('debug', true);

[#82127] Tuesday, November 6, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alessandrol

Total Points: 286
Total Questions: 107
Total Answers: 109

Location: Uzbekistan
Member since Sat, Feb 27, 2021
3 Years ago
;