Adam
Adam

Reputation: 3288

Meteor is Slow to Access a MongoDB

Background - I have a collection with 7,933 documents. I'm testing on a local host with a local mongodb server running. I also get the same issue on localhost with a remote mongodb server.

Problem - When running a simple db.collection.find().count(), meteor returns 0 documents found at first, incrementing to 7,933 over the course of about 6 seconds.

Console -

Count at: 1402440532060 is 0
Count at: 1402440533061 is 322
Count at: 1402440534064 is 1293
Count at: 1402440535087 is 2799
Count at: 1402440536557 is 4666
Count at: 1402440537696 is 7933
Count at: 1402440538697 is 7933
Count at: 1402440539699 is 7933
Count at: 1402440540701 is 7933
Count at: 1402440541702 is 7933 

App Structure -

/client/foo.html
/clint/foo.css
/client/foo.js
/lib/collections.js
/server/server.js

Code -

/lib/collections.js:

fooCollection = new Meteor.Collection('fooCollection');

/server/server.js:

Meteor.publish("fooDB", function () {
  return fooCollection.find();
});

/client/foo.js:

Deps.autorun(function() {
  Meteor.subscribe("fooDB");
});

var counter = 0;
var i = setInterval(function(){
    var ts = Date.now();
    console.log("Count at: " + ts + " is " + fooCollection.find().count());

    counter++;
    if(counter === 10) {
        clearInterval(i);
    }
}, 1000);

Upvotes: 1

Views: 1210

Answers (1)

Andrew Mao
Andrew Mao

Reputation: 36940

The data in the collection is being streamed from the server to the client. It won't teleport over there instantly. The client's local collection is just a cache or view of the server's publication, and is not an authoritative version of the database.

To make sure that your collection.count() is accurate, you'll want to make sure the subscription(s) sending data are ready. See the docs for http://docs.meteor.com/#meteor_subscribe.

Upvotes: 2

Related Questions