I have a project am working on. I have a comment schema, a likes schema and a blog schema. All three are declared as separate schemas but the likes and comments schema are then nested into the blogs schema as children i.e comments: [CommentSchema]. Now i have a page where when someone clicks on a blog it displays all the comments together with the blog. This the code that gets the blog Blog.findById(id).populate('user', 'username').exec(function(err, blog). Now in the comments array there is a key called commOwner that refers to the objectid from another schema called user just like the blog schema has a reference key too as u can see from the code. I am trying to display the gravatar and username of each person that made a comment based on the reference key commOwner that's in the comments schema but i don't know how to go about it. I want to also be able to populate the usernames and gravatar of those who commented on the blog in that same code where i did it for the blog. Please can someone help me with this. below is the code for all my schemas
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/*Comment schema*/
var CommentSchema = new Schema({
commOwner: {
type: Schema.ObjectId,
ref: 'User'
},
commbody: {
type: String,
default: '',
trim: true,
//required: 'Comment cannot be blank'
},
updated: {
type: Date,
default: Date.now
}
});
/**
* Likes Schema
*/
var LikeSchema = new Schema({
score : {
type: Number,
default: 0
},
user: {
type: Schema.ObjectId,
ref: 'User'
}
});
/**
* Blog Schema
*/
var BlogSchema = new Schema({
created: {
type: Date,
default: Date.now
},
title: {
type: String,
default: '',
trim: true,
required: 'Title cannot be blank'
},
content: {
type: String,
default: '',
trim: true
//required: 'Content cannot be blank'
},
user: {
type: Schema.ObjectId,
ref: 'User'
},
comments: [CommentSchema],
likes: [LikeSchema]
});
mongoose.model('Blog', BlogSchema);