Created
May 4, 2012 08:01
-
-
Save wilsonpage/2593159 to your computer and use it in GitHub Desktop.
How to populate model ref property after instantiation?
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 commentsSchema = new Schema({ | |
content: { type: String }, | |
_user: { type: Schema.ObjectId, ref: 'users' }, | |
}); | |
//... | |
// Get a comment without populating | |
CommentsModel | |
.findOne({ _id: COMMENT_ID }, function(err, comment) { | |
UsersModel | |
.findOne({ _id: comment._user }, function(err, user) { | |
// Attempt to set user model on comment (like .populate) | |
comment.set('_user', user); | |
console.log(comment); | |
//=> comment._user is still just the original object id. | |
// .set() command seems to have been ignored by Mongoose. | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If the comment._user was not populated on the original find, what is the best method to mimic populate()? How can I force mongoose to set the user model on the comment._user property? I think it's something to do with types, not sure how to work round it though.