"index: { unique: true }" not working

1,073 views
Skip to first unread message

Clint H.

unread,
Aug 18, 2011, 4:46:21 PM8/18/11
to Mongoose Node.JS ORM
I can't get Mongoose to create a unique index for my model, which
looks like this:

var UserSchema = new mongoose.Schema({
my_id: {
type: String,
index: { unique: true },
required: true
},
name: { type: String }
});

var UserModel = mongoose.model('users', UserSchema);

New documents are inserted just fine, but the index/unique-constraint
is never created; I can insert duplicates and don't see the index when
I run db.users.getIndexes().

Dropping the database and restarting mongod doesn't seem to help.

Lastly, I have verified that if I manually run ensureIndex via shell,
the unique constraint works. I'd like to have Mongoose do it for me,
though.

Any help would be appreciated. Thanks.

[OSX 10.6, Node 0.5.1, MongoDB 1.8.2, Mongoose 1.8.2, mongodb 0.9.6-7]

Clint H.

unread,
Aug 18, 2011, 10:27:10 PM8/18/11
to Mongoose Node.JS ORM
Solved. The problem was that something else was causing my program to
exit (silently, without an error) BEFORE the event loop actually ran
the statement for creating the index. After fixing the early exit
issue, everything worked fine.
Reply all
Reply to author
Forward
0 new messages