Mongoose Not Creating Indexes

Posted by wintzer on Stack Overflow See other posts from Stack Overflow or by wintzer
Published on 2012-09-17T03:35:04Z Indexed on 2012/09/17 3:37 UTC
Read the original article Hit count: 189

Filed under:
|
|

I have been trying all afternoon to get my node.js application to create MongoDB indexes properly. I am 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. Please tell me what I'm doing 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);
});

© Stack Overflow or respective owner

Related posts about node.js

Related posts about mongodb