node.js - Populating field values for referred documents in aggregate call in Mongoose -
i have 2 base schemas user , course
//user model
var userschema = new schema({ userid: { type: string, default: '', required: 'please provide userid.', index : true }, name :{ type: string, default: '', trim : true } });
//course schema
var courseschema = new schema({ title: { type: string, default: '', required: 'please fill course title', trim: true, index: true }, description: { type: string, default: '', trim: true }, category: { type: string, ref: 'category', index: true } });
and usercourseprogress schema storing each user's course progress.
var usercourseprogress= new schema({ userid: { type: string, ref:'user', required: 'please provide user id', index:true }, courseid: { type: schema.types.objectid, ref: 'course', required: 'please provide course id', index: true }, timespent: { type: number, //seconds default: 0 }, score: { type: number } });
now have aggregate results such on usercourseprogress schema on course level such show result : - {course name}( {no_of_users_taken_course} ) [e.g. course (15) ]
history.aggregate([{ $group: {"_id": {"primary": "$courseid"}, "popularitycount": {$sum: 1}}}, {$sort:{"popularitycount":-1}}, {$project:{"_id":0, "courseid":"$_id.primary", "count":1}} ])
now want populate course title course schema through reference, after going through list of aggregation operators(here).i not able figure out way so.
can suggest way populate data other collections ??
Comments
Post a Comment