Alizeec
Alizeec

Reputation: 41

Collection.find() very slow with other BDD Meteor React

I'm working on an app with Meteor and React. I'm trying to have the content of inventories_array from an external MongoDB database, but it's extremely slow. I wait 7 seconds, I have one object, I wait 5 seconds, two other objects, etc...

Spaces = new Mongo.Collection("Space");
Properties = new Mongo.Collection("Property");
Inventories = new Mongo.Collection("Inventory");
if (Meteor.isClient) {
    Meteor.subscribe("Property");
    Meteor.subscribe("Space");
    Meteor.subscribe("Inventory");  
    Tracker.autorun(function() { 

        inventories_array = Inventories.find({quantityBooked: 2},{fields: {priceTaxExcl: 1, property: 1, space: 1}}).fetch();
    console.log(inventories_array);

}

if (Meteor.isServer) {
    Meteor.publish("Property", function () {
    return Properties.find();
  });

  Meteor.publish("Space", function () {
    return Spaces.find();
  });
  Meteor.publish("Inventory", function () {
    return Inventories.find();
  }); 
}

The Inventory Object:

{
 ...
 "property" : ObjectId("..."),
 "space" : ObjectId("..."),
 "quantityBooked":2,
 "priceTaxExcl":...,
 ...
}

I launch the app with MONGO_URL=mongodb://127.0.0.1:27017/mydb meteor run

Any ideas why it's so slow?

Upvotes: 2

Views: 173

Answers (1)

Michel Floyd
Michel Floyd

Reputation: 20226

If you look at your network tab in the inspector you'll see all the data flowing from the server to the client for each subscription and you'll be able to judge both how large it is and how long it takes.

I'd recommend at a first step that you alter your Inventory publication as follows:

Meteor.publish("Inventory", function () {
  if ( this.userId ){ // only return data if we have a logged-in user
    return Inventories.find({quantityBooked: 2},{fields: {priceTaxExcl: 1, property: 1, space: 1}});
  } else {
    this.ready();
  }
});

This way your server is only sending the required fields from the required documents (assuming you only want docs where {quantityBooked: 2}.

Upvotes: 1

Related Questions