guillefix
guillefix

Reputation: 379

Meteor server response slow when importing collection

I have a MongoDB collection Foods in my Meteor app with about 8000 entries with almost 1000 fields each.

For some reason, since I included it, the response time when I call a server method from the client is very slow (seconds). For debugging, I've been removing things one by one. Now, I don't use the collection in any of the functions involved (I've even replaced the server method with just a console.log), and yet if I add the line import { Foods } from '../imports/collections.js'; to the server, the response is slow, and if I don't it is fast.

Does anyone have any idea why this could be?

Upvotes: 1

Views: 149

Answers (2)

Jankapunkt
Jankapunkt

Reputation: 8423

Note: OP has already answered his question satisfactory. However, the following information should be pointed out, especially for those who face similar issues in context of controlling Meteor's autopublish behavior.


As long as autopublish exists in your packages list, each collection publishes to the client at the very moment, your Mongo.Collection is created.

See this code of the Mongo.Collection constructor.

Autopublish is then triggered, even just by importing your file that contains a Mongo.Collection constructor to be called (which is very likely your case).

However, you dont need to remove the autopublish package if you only want this one collection to prevent auto publishing.

Your Mongo.Collection constructor accepts as second parameter an object with options. To prevent auto publish only for this collection (while having autopublish active) you can add the following option:

{
    _preventAutopublish: true,
    // ... other options if desired
}

An example for your given Foods collection could be

export const Foods = new Mongo.Collection('foods', {_preventAutopublish: true});

Although the option is not documented, it works and comes in very handy when prototyping.

However, keep in mind that autopublish is not secure and should never be present in a release that is expected to be deployed on a server.

Upvotes: 3

guillefix
guillefix

Reputation: 379

So the answer was easy.

As said in the Meteor docs,

By default, Meteor automatically publishes every document in your collection to each connected client. To turn this behavior off, remove the autopublish package

So, simply by importing the collection, Meteor interprets that you want to publish it all, and that is what was making it slow, even if I wasn't explicitly using it.

Upvotes: 0

Related Questions