Created
December 17, 2014 21:22
-
-
Save JakeDluhy/0d057208e7216eceb7bd to your computer and use it in GitHub Desktop.
WatchMeCode Episode 53 Indexing Difficulty
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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