2012-10-08 92 views
4

我最近开始使用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()几次之后,会创建它们。

谢谢!

+0

奇怪。你能发布定义你的'myCollModel'模式的代码吗? – JohnnyHK

回答

0

当定义一个字段是ObjectId引用另一个集合中的文档时,ref属性的值是模型的字符串名称,而不是模型本身。因此,它应该是这样,而不是:

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

我有一种感觉,是否创建与否type指标,失败的定时影响。

+1

谢谢我试过,但仍然有同样的问题。我甚至完全删除了该字段,但索引仍未创建。 – Nitzo

0

确实,我有同样的问题。

然后我复制ensureindex调试信息到蒙戈终端尝试一下,命令被送到(就像从猫鼬控制台中显示),但创建失败,由于蒙戈的问题:

{ 
    "err" : "ns name too long, max size is 128", 
    "code" : 10080, 
    "n" : 0, 
    "connectionId" : 78, 
    "ok" : 1 
} 

然后我给出选项{name:'super_index'},然后完成。

希望它对你有帮助!