Skip to content

Instantly share code, notes, and snippets.

@JakeDluhy
Created December 17, 2014 21:22
Show Gist options
  • Save JakeDluhy/0d057208e7216eceb7bd to your computer and use it in GitHub Desktop.
Save JakeDluhy/0d057208e7216eceb7bd to your computer and use it in GitHub Desktop.
WatchMeCode Episode 53 Indexing Difficulty
I ran into a problem with unique indexing using Mongoose. Essentially, even though I set the index: {unique: true} flag it still allows duplicate documents.
var mongoose = require('mongoose'),
User = require('./user');
mongoose.connect('mongodb://localhost/WatchMeCode', function(err) {
if (err) { throw err; }
var user = new User({
email: "[email protected]"
});
user.save(function(err, user) {
if (err) { throw err; }
console.log(user);
process.exit();
});
});
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var User;
var UserSchema = new Schema({
firstName: {type: String},
lastName: {type: String},
email: {type: String, required: true, index: {unique: true}},
password: {type: String}
});
User = mongoose.model('User', UserSchema);
module.exports = User;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment