Created
November 24, 2012 01:28
-
-
Save JeanSebTr/4137973 to your computer and use it in GitHub Desktop.
Deserializing Mongoose document (i.e. JSON to Doc)
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 db = /* ... */ | |
var obj = { | |
_id: '21245b528adfad75b0480baf', | |
email: 'jeansebtr@xpensia.com', | |
username: 'jeansebtr', | |
fullname: 'Jean-Sébastien Tremblay' | |
}; | |
var User = db.model('User'); | |
var user = new User(undefined, undefined, true).init(obj); | |
user.fullname = 'Jean-Seb Tremblay'; | |
user.save(function(err) { | |
console.log(err); // null | |
}); |
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'); | |
var Schema = mongoose.Schema; | |
var User = module.exports = new Schema({ | |
// ... | |
}); | |
User.statics.deserialize = function(txt) { | |
var User = this.model('User'); | |
var obj = JSON.parse(txt); | |
return new User(undefined, undefined, true).init(obj); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment