I recently started to use Mongoose (v.3.2.1) and I am having problems with the indexes.
I define a couple of indexes on my schema ( Schema.path('attr').index(true) ) and they are not being created in the DB. (I run db.collection.getIndexKeys() in the shell and I only see the _id index).
Note that sometimes some/all of the indexes are created.
I turned on debugging and I see the ensureIndex() running:
mongoose.set('debug', true);
Mongoose: myColl.ensureIndex({ type: 1 }) { safe: true, background: true }
Mongoose: myColl.ensureIndex({ created: 1 }) { safe: true, background: true }
Mongoose: myColl.ensureIndex({ updated: 1 }) { safe: true, background: true }
I also listened for errors:
mongoose.connection.on('error', function(err) {
console.error('MongoDB error: %s', err);
});
myCollModel.on('index',function(err) {
console.error('MongoDB error: %s', err);
});
I can see my inserts and queries in the console but no error.
Any help will be greatly appreciated!
Thanks,
Nitzan Bar
My Schema is defined like this:
myColl = new Schema();
myColl.add({
text : { type: String, required : true }
, shortText: { type: String }
, type : { type: String , index: true}
, correctAnswerId : { type: ObjectId, ref: QuestionAnswer}
, answers: { type: [ QuestionAnswer ] }
});
In this case 'type' index is not created. Note that sometimes after running the ensureIndexes() for a couple of times they are created.
Thanks!