我最近开始使用Mongoose(v.3.2.1),但在索引方面遇到了问题。
我在模式上定义了一些索引(Schema.path('attr').index(true)),但它们并没有在数据库中创建。(我在shell中运行db.collection.getIndexKeys(),只看到_id索引。)
请注意,有时候会创建一些或全部索引。
我打开了调试功能,看到ensureIndex()正在运行:
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 }
我也听取错误信息:
mongoose.connection.on('error', function(err) {
console.error('MongoDB error: %s', err);
});
myCollModel.on('index',function(err) {
console.error('MongoDB error: %s', err);
});
我可以在控制台中看到我的插入和查询,但没有错误。
任何帮助将不胜感激!
谢谢,
Nitzan Bar
我的模式定义如下:
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 ] }
});
在这种情况下,'type'索引并未被创建。请注意,有时在运行ensureIndexes()几次后它们才会被创建。
谢谢!
myCollModel
模式的代码吗? - JohnnyHK