How to new mutliple datasource form same collections?

I want get different data form same collections ,but the way don’t work such as:

const Articles = new Mongo.Collection(‘articles’);
const Articles2 = new Mongo.Collection(‘articles’);

Meteor.publish(‘articles’, function publishFunction() {
return Articles.find({}, {sort: {date: -1}});
Meteor.publish(‘articlesByUserId’, function publishFunction(userId) {
return Articles2.find({userId:userId}, {sort: {date: -1}});

console info Error: A method named ‘/articles/insert’ is already defined

You cannot define two collections referring to the same collection name (as you have seen).

You can have multiple subscriptions to a collection, each of which return different views, however the resulting client-side collection will be the merged set of publications.

You should read the Meteor Guide on Publications and Data Loading to get a better understanding around how Meteor pub/sub works :slight_smile:

thanks,you’ve eliminated my confusion.

I wish the official supply the feature in the future.

1 Like