Hi. I too am working on an app where users can join different events. I have multiple different supscriptions depending on what the user wants to see. Sometimes he wants to see which events he’s joined, other times he might want to se all events nearby, or which events his friends is participating in.
Inside my “events”-collection I have a field which is an array called participants.
I’ve listed out 3 of the fields in a document in my collection, creatorId, creatorName and the participants field (which is what we think is interesting here)
“createdBy”: “maqMe7E75T8i25QgN”,
“creatorName”: “test3”,
“participants”: [
{
“userId”: “bx6MuZHgdotH3nJzn”,
“hasRide”: true,
“tickets”: 1
},
{
“userId”: “REH42ye7XpptZJNXC”,
“hasRide”: true,
“tickets”: 1
},
{
“userId”: “numegLhk5HSjDdwCx”,
“hasRide”: true,
“tickets”: 1
}
],
Each user gets a position in the array and has 3 fields.
- userId (duh)
- if the user has a ride or not (app is targeted to old people who may struggle to get to somewhere)
- tickets (if they’re bringing friends that does not have an account they can add up to 5 extra tickets)
so… in this use case we have 3 users that has joined the specific event. the subsription code I run to let the users see every event they have joined is as following:
Meteor.publish('joinedEvents', function (per_page) {
check(per_page, Number);
if (!this.userId) {
return this.ready();
}
let options = {sort: {datetime: 1}, limit: per_page};
return Events.find({'participants.userId': this.userId},
options
);
});
This works for me… There might be a better way (in which case I’d love to hear it).
What’s key to know if you’re new to subscriptions is that if you try to join on other collections inside the subscription it’ll stop being reactive.
I believe the following article should still be relevant (please correct me if I’m wrong)
Reactive Joins in Meteor
I’ve needed reactive joins a couple of times and I’ve ended up using the Publish Composite package, although this may be a good deal ‘heavier’ than normal subscriptions.
If you post a code snippet of your subscription that could may help people see what you’ve done and if there is a better way.
Please bear in mind that I’m no expert. I’ve still got a lot to learn so take what I say with a grain of salt
EDIT: I forgot to mention… As you can see, my subscription fetches from the ‘events’ collection. This may not be the best option if there’s hundreds of thousands of events which each has thousands upon thousands of participants (unlikely in my case)
It may be a good idea to also store the eventIds inside the user-document so that you know exactly which events you’re going to fetch. If the user already has the id’s of the events he’s attending you could send a list of these to the subscription and just fetch the events with ID corresponding to what it received. Note that this may result in a bigger ‘cleanup-job’ so if you go with this method, ensure you clean up your fields in your different collections if you were to delete an event or a user…