meteor-joins icon indicating copy to clipboard operation
meteor-joins copied to clipboard

Join Field problem

Open m-korkut opened this issue 7 years ago • 2 comments

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}}

m-korkut avatar Mar 21 '17 08:03 m-korkut

@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;
  }
});

:)

perak avatar Mar 21 '17 08:03 perak

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 ?

m-korkut avatar Mar 21 '17 13:03 m-korkut