1

Was checking out Mongoose and the relatively new populate method. Seems to work perfect when populating from the child to the parent like so:

var Comment = new Schema({
    Message: { type: String }
    User: { type: ObjectId, ref: 'User' }
});
var User = new Schema({
    Username: { type: String }
    Comments: [{ type: ObjectId, ref: 'Comment' }]
});

The following works as expected.

Comment.find({}).populate({ path: 'User' }).exec(function (err, comments) {
    if(err) handle(callback);
    // no error do something with comments/user.
    // this works fine and populates the user perfectly for each comment.
});

User.findOne({ Username: "some username"}).populate('Comments').exec(function (err, user) {
    if(err) handle(callback);
    // this throws no errors however the Comments array is null. 
    // if I call this without populate I can see the ref ObjectIds in the array.
});

The fact that the ObjectIds are visible without calling populate on the User model/schema and the fact that I can populate just fine from the child side ref makes it appear that the configuration is correct yet no joy.

The above schemas were shorted so as not to post a mile long list of code ( I hate that!!!). Hoping I'm missing something simple.

3
  • Have you tried uncapitalizing the path name? It's highly unlikely that this is the problem, but capitalized path names are not idiomatic and it's possible that mongoose is lowercasing them somewhere as part of a normalization process. Just grasping at straws here as, aside from that, your example is textbook. Commented May 28, 2013 at 3:01
  • as to textbook that's what I"m thinking it's retarded. gotta be something stupid right. I like objects/props when they are representing a type (.net guy so like a class) to be cap, just for consistency. Wondering if u could be right. I'll try different case on the ref properties. Beating my head on this though. Commented May 28, 2013 at 3:18
  • no dice on the lowercase... Commented May 28, 2013 at 3:22

1 Answer 1

1

O.K. sorted this out. Unfortunately this like many things the docs are not always crystal clear. In order to use "populate" in both directions. That being Child to Parent and conversely Parent to child you MUST still push your child item to the parent array. These are documents of course and not a relational database so essentially the populate is a pseudo relational relationship or at least that's how I see it so although I had the components correctly configured the sequence I had was originally incorrect. Bottom line it wasn't too complicated I just had to think logically about it. So here you go for the next joe...

NOTE: My original question's find methods are correct it was the initial saving to the db that was inaccurate and causing the Parent to Child population.

 user.save(function (err) {
    comment.User = user._id;
    comment.save(function (err) {
       user.Comments.push(comment);
       user.save(function (err) {
          if (err) {                                
              res.json(400, { message: err + '' });
          } else {
              res.json(200, { message: 'success' });
          }
      });             
    });             
 });                
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.