【问题标题】:Meteor is Slow to Access a MongoDBMeteor 访问 MongoDB 很慢
【发布时间】:2014-06-11 07:21:21
【问题描述】:

背景 - 我有一个包含 7,933 个文档的集合。我正在运行本地 mongodb 服务器的本地主机上进行测试。我也在 localhost 上使用远程 mongodb 服务器遇到了同样的问题。

问题 - 运行简单的 db.collection.find().count() 时,meteor 返回最初找到的 0 个文档,在大约 6 秒内增加到 7,933 个。

控制台 -

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 

应用结构 -

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

代码 -

/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);

【问题讨论】:

  • 我已经用多个集合对此进行了测试。此外,在 mongo 实例中运行 db.collection.find().count() 始终返回正确的计数。

标签: javascript mongodb meteor


【解决方案1】:

集合中的数据正在从服务器流式传输到客户端。它不会立即传送到那里。客户端的本地集合只是服务器发布的缓存或视图,并不是数据库的权威版本。

为确保您的collection.count() 准确无误,您需要确保发送数据的订阅准备就绪。请参阅http://docs.meteor.com/#meteor_subscribe 的文档。

【讨论】:

  • 看来我没有做作业。我可能应该注意依靠服务器;使用流星调用返回计数;并且不向客户发布所有文档。
  • 如果你不需要把所有的文件都发给客户,你绝对不应该在那里做计数!您还可以在出版物中发送计数;见stackoverflow.com/q/14656567/586086。
猜你喜欢
  • 1970-01-01
  • 2015-02-02
  • 1970-01-01
  • 2017-01-18
  • 1970-01-01
  • 2014-06-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多