背景 - 我有一个包含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);
答案 0 :(得分:2)
集合中的数据正从服务器流式传输到客户端。它不会立即传送到那里。客户端的本地集合只是服务器发布的缓存或视图,并不是数据库的权威版本。
为确保您的collection.count()
准确无误,您需要确保发送数据的订阅准备好。请参阅http://docs.meteor.com/#meteor_subscribe的文档。