Users
- id
- events_attended (array of event ids)
Events
- id
- users_attended (array of user ids)
If you want to get events a user attended, do a query like:
1) get the user.events_attended array
2) events.find({_id:{$in: [user.events_attended] }})
3) now you have a nice json object of all events the user has been to
If you want to get all users that went to an event:
1) get the event.users_attended array
2) users.find({_id:{$in: [event.users_attended] }});
3) now you have a nice json object of all users who attended and event
Storing event_ids as an array in the events field defeats the whole purpose of organizing your data into rich documents.
In real life, you very well could build something where the events info was built straight into the user record.
db.events.find({hosted_by: MY_ID})
db.events.find({invited: MY_ID}) // invited is an array of usersIn this case, the events in the user document are events that the user is HOSTING.
There would be another object in our data model to represent the people that got invites to the event. These 'recipients' could be another collection or a list embedded inside each event object.
db.users.find({'events.invites.email': 'sv123@gmail.com'})
and that would return all the users that had invited you to an event.
Now, like I said you're testing my example. When these kinds of requirements are taken into account, you'd probably want to have a separate collection for events. Then you could do:
db.events.find({'invites.email': 'sv123@gmail.com'})