Need populate methods such that I can show second-level detail for each top-level item

23 views
Skip to first unread message

Michael Murray

unread,
Apr 11, 2014, 1:18:41 AM4/11/14
to mongoo...@googlegroups.com
Hi all - I'm a bit new to mongoose so there may be a straightforward answer to this but I haven't yet found it...

There is a full gist of my schemas/models herehttps://gist.github.com/FreeAssoc/10441656

But in short, I have three models -- Post, Opinion and Comment -- that are related thusly:

Post is a top-level "statement".  It has 0-n Opinions about that statement, structured as an array of ObjectIds on the Opinion model 

var PostSchema = new Schema({
(...)
opinions: [ { type: Schema.ObjectId, ref: 'Opinion' } ]
});

while Opinions have 0-n Comments about each Opinion:

var OpinionSchema = new Schema({
(...)
comments: [ { type: Schema.ObjectId, ref: 'Comment' } ]
});

When I view each post I want to populate such that I include all opinions and all comments on opinions with each post, and for various business reasons I want to keep posts, opinions and comments in separate (not embedded) models.

What works:

  • Post.user info is included through ObjectId (works)
  • detail for each of Post.opinions is included through ObjectIds (works)
What I can't figure out:
  • detail for post.opinions[n].user is shown for each opinion.user (doesn't yet work)
  • detail for post.opinion[n].comments -- including the comment.user -- is shown (dosn't yet work)

so in short my issue is that I need to show detail from sub-subdocuments on Post through .populate :).

Advice/pointers welcome.

Thanks - Michael

Reply all
Reply to author
Forward
0 new messages