Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Join Field problem #14

Open
m-korkut opened this issue Mar 21, 2017 · 2 comments
Open

Join Field problem #14

m-korkut opened this issue Mar 21, 2017 · 2 comments

Comments

@m-korkut
Copy link

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

@perak
Copy link
Owner

perak commented Mar 21, 2017

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

:)

@m-korkut
Copy link
Author

m-korkut commented Mar 21, 2017

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 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants