我正在使用Meteor和React开发应用程序。 我正在尝试从外部MongoDB数据库获取inventories_array的内容,但它非常慢。我等了7秒钟,我有一个物体,我等了5秒钟,还有两个其他物体等......
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();
});
}
清单对象:
{
...
"property" : ObjectId("..."),
"space" : ObjectId("..."),
"quantityBooked":2,
"priceTaxExcl":...,
...
}
我使用MONGO_URL=mongodb://127.0.0.1:27017/mydb meteor run
任何想法为什么这么慢?
答案 0 :(得分:1)
如果您在检查器中查看网络选项卡,您将看到每个订阅从服务器流向客户端的所有数据,并且您将能够判断它的大小和长度需要。
我建议您在第一步修改Inventory
出版物,如下所示:
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();
}
});
这样,您的服务器只发送所需文档中的必填字段(假设您只需要{quantityBooked: 2}
的文档。
答案 1 :(得分:0)
非常感谢!它适用于
Meteor.publish("some-inventories", function () {
return Inventories.find({quantityBooked: 2});
});
(我还没有管理用户) 但是当我在服务器上的find()中放置{fields:{priceTaxExcl:1,property:1,space:1}}时,我什么也得不到。知道为什么吗?
Brett McLain,谢谢你的帮助,并向我推荐Kadira!