Reputation: 3608
Hi I have the following code as part of my application in which I have to enable indexing
var db = mongoose.createConnection(config.database.address, config.database.dbName);
var testSchema = new mongoose.Schema({
status : String,
loc : {lon: Number, lat: Number },
createDate : {type: Date, default: Date.now}
});
exports.registerEmployee = function (objEmployee , callback)
{
var employee = db.model(objEmployee.categoryName, testSchema );
// Saves data
}
How to enable indexing for the above schema? Since the collection name (objEmployee.categoryName) is being created dynamically I'm kind of stuck here because for different category there will be a new collection get created. When we create new collection we have to enable Indexing also.
How to do this ?
Upvotes: 0
Views: 1045
Reputation: 311835
The fact that the collection name is dynamic doesn't affect how you'd create an index on loc
. You declare the index as part of the loc
field definition of testSchema
and Mongoose will take care of creating that index on each collection you register the schema for via db.model
. As for the index definition, you can specify that right in the schema:
loc: {type: {lon: Number, lat: Number}, index: '2d'},
Upvotes: 1