我最近开始使用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酒吧
我的架构的定义是这样的:
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 ] }
});
在这种情况下, '类型' 指数不创建。请注意,有时在运行ensureIndexes()几次之后,会创建它们。
谢谢!
奇怪。你能发布定义你的'myCollModel'模式的代码吗? – JohnnyHK