【发布时间】: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