猫鼬不创建索引

2024-05-21

我最近开始使用 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);
});

我可以在控制台中看到我的插入和查询,但没有错误。

任何帮助将不胜感激!

Thanks,

尼赞酒吧

我的架构定义如下:

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() 几次后它们会被创建。

Thanks!


当定义一个作为对另一个集合中文档的 ObjectId 引用的字段时,ref属性的值是模型的字符串名称,而不是模型本身。所以它应该看起来像这样:

correctAnswerId : { type: ObjectId, ref: 'QuestionAnswer' }

我有一种感觉,失败的时机正在影响type索引是否创建。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

猫鼬不创建索引 的相关文章

随机推荐