Created
June 22, 2011 16:19
-
-
Save jeremys/1040469 to your computer and use it in GitHub Desktop.
Index/Save conflict
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; | |
mongoose.connect('mongodb://localhost/db'); | |
var User = new Schema({ | |
email: { 'type': String, 'required': true, 'index': { 'unique': true, 'background': true } }, | |
}); | |
User = mongoose.model('User', User); | |
var newUser = new User(); | |
newUser.email = 'john@doe.com'; | |
newUser.save(function (err) { | |
console.log('saved'); | |
}); | |
newUser = new User(); | |
newUser.email = 'john@doe.com'; | |
newUser.save(function (err) { | |
console.log('saved'); | |
}); |
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
Model.init = function () { | |
// build indexes | |
var self = this | |
, indexes = this.schema.indexes | |
, count = indexes.length; | |
indexes.forEach(function (index) { | |
self.collection.ensureIndex(index[0], index[1], function(){ | |
--count || self.emit('index'); | |
console.log('index finished'); | |
}); | |
}); | |
}; |
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
$ node conflict.js | |
saved | |
saved | |
index finished |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment