Publish Two Cursors from Same Collection

How can I publish two cursors from the same collection but with different queries that might contain duplicate results? For example, can I publish both cursors from the collection “Rooms” like this?

Rooms.find({users: this.userId}, {fields: {_id: 1, name: 1, users: 1, pending: 1}});
Rooms.find({}, {fields: {_id: 1, name: 1}});

No you can’t pusblish two cursor from same collection, From the docs

If you return multiple cursors in an array, they currently must all be from different collections. We hope to lift this restriction in a future release.

Thanks for your reply. Will instead using two publish functions, one for each cursor, work? Will Meteor automatically combine the two cursors?

yeah, read this to understand about publish and subscribe

1 Like

There are packages allowing this feature, such as meteor-smart-publish and meteor-publish-composite (I think). Notice that those packages are primarily designed for reactive joins and introduce server-side overhead.

1 Like

Thank you! I think it works great! I wonder why Meteor does not allow returning multiple cursors in an array now.

+1 Need this implemented in Meteor.

Both packages proposed seems nice, but one is “do not use in prod” and the other one less flexible.

First and foremost I’ve usually found you have to go pretty far before needing to separate, consider how you could use and nest the $and and $or operators.

Where we’ve need multiple cursors from the same collection we’ve usually hit the issue of the very smart auto-merging on the client, which can easily be subverted by creating and filling a client-side only collection for one of the publications.

In your example above, it seems redundant to have two cursors, you could easily just use the second, with the firsts fields, and then query a subset on the client. If these need to be displayed and managed independently, then I’d recommend the local client side collection route.