猫鼬没有创建索引 [英] Mongoose Not Creating Indexes

查看:108
本文介绍了猫鼬没有创建索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试创建MongoDB索引。使用Mongoose ODM并在我下面的模式定义中,我将username字段设置为唯一索引。所有集合和文档都被正确创建,它只是不起作用的索引。所有文档都说应该在启动时运行ensureIndex命令来创建任何索引,但是没有任何索引。如果重要的话,我正在使用MongoLab进行托管。我也多次放弃收藏。有什么问题。

Trying to create MongoDB indexes. Using the Mongoose ODM and in my schema definition below I have the username field set to a unique index. The collection and document all get created properly, it's just the indexes that aren't working. All the documentation says that the ensureIndex command should be run at startup to create any indexes, but none are being made. I'm using MongoLab for hosting if that matters. I have also repeatedly dropped the collection. What is wrong.

var schemaUser = new mongoose.Schema({
    username: {type: String, index: { unique: true }, required: true},
    hash: String,
    created: {type: Date, default: Date.now}
}, { collection:'Users' });

var User = mongoose.model('Users', schemaUser);
var newUser = new Users({username:'wintzer'})
newUser.save(function(err) {
    if (err) console.log(err);
});


推荐答案

挂钩'索引' 模型上的事件,以查看在异步创建索引时是否发生任何错误:

Hook the 'index' event on the model to see if any errors are occurring when asynchronously creating the index:

User.on('index', function(err) {
    if (err) {
        console.error('User index error: %s', err);
    } else {
        console.info('User indexing complete');
    }
});

此外,通过调用以下方式启用Mongoose的调试日志记录:

Also, enable Mongoose's debug logging by calling:

mongoose.set('debug', true);

调试日志记录将显示 ensureIndex 打电话给你创建索引。

The debug logging will show you the ensureIndex call it's making for you to create the index.

这篇关于猫鼬没有创建索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆