meteor-joins
meteor-joins copied to clipboard
Join Field problem
hii , example my two collections 1-studentCollection => There is student information 2- reportCollection => There is report information . Relationship = reportCollection (student_id) ---> studentCollection (_id) join using ,I want to reverse studentCollection --> reportCollection. How can I do it.
studentCollection { _id,name} reportCollection{ _id,student_id,report}}
@cezerii this package allows you to list reports with student data included:
{
_id,
student_id,
report,
student: {
_id,
name
}
}
You cannot do opposite automatically, but you can use transform
and do it yourself:
Server side: publish both reports and students
Meteor.publish("students_and_reports", function() {
return [
Students.find(),
Reports.find()
];
});
Subscribe client side:
Meteor.subscribe("students_and_reports");
And read data like this:
var studentsWithReports = Students.find({}, {
transform: function(doc) {
doc.report = Reports.findOne({ student_id: doc._id });
return doc;
}
});
Or, if one student has multiple reports:
var studentsWithReports = Students.find({}, {
transform: function(doc) {
doc.reports = Reports.find({ student_id: doc._id }).fetch();
return doc;
}
});
:)
Thank you for your solution. I did it in a different way.I use Virtual mongo collection. Do you intend to improve join structure like sql join ?