Skip to content

Instantly share code, notes, and snippets.

@cmalven
Last active December 28, 2015 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmalven/7469244 to your computer and use it in GitHub Desktop.
Save cmalven/7469244 to your computer and use it in GitHub Desktop.
Joining collections in Meteor/Mongo
# In an Iron Router Route (requires that the below publication is also set up)
# =====================================
Router.configure
layoutTemplate: 'layout'
loadingTemplate: 'loading'
Router.map ->
@route 'member_show',
path: '/members/:_id',
template: 'members_show',
waitOn: ->
return [
Meteor.subscribe('members'),
Meteor.subscribe('documents', @params._id)
]
data: ->
return {
member: Members.findOne({_id: @params._id})
documents: Documents.find()
}
# In typical publish
# =====================================
Meteor.publish 'documents', (member_id) ->
return Documents.find() unless member_id?
member = Members.findOne
_id: member_id
return Documents.find
_id: {$in: member.documents}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment