Mongodb 为什么我的Collection.find().count()在客户端总是返回0?
我收集了170万份文件。在服务器端控制台上执行Mongodb 为什么我的Collection.find().count()在客户端总是返回0?,mongodb,meteor,Mongodb,Meteor,我收集了170万份文件。在服务器端控制台上执行count()时,我得到了正确的结果 meteor:PRIMARY> db.postcodes.find().count(); 1737697 meteor:PRIMARY> 而在浏览器控制台上,count() 不安全的程序包尚未删除。和count()和findOne()正在为其他较小的集合工作。目前代码不多。除了默认的html、js、css之外。只有几行代码存在。我的model.js位于它自己的文件夹中(既不在服务器中也不在客户机中)
count()
时,我得到了正确的结果
meteor:PRIMARY> db.postcodes.find().count();
1737697
meteor:PRIMARY>
而在浏览器控制台上,count()
不安全的
程序包尚未删除。和count()
和findOne()
正在为其他较小的集合工作。目前代码不多。除了默认的html、js、css之外。只有几行代码存在。我的model.js
位于它自己的文件夹中(既不在服务器中也不在客户机中),该文件夹
PostCodes = new Mongo.Collection('postcodes');
Hello = new Mongo.Collection('hello');
我现在所有的收藏都是
meteor:PRIMARY> db.getCollectionNames();
[
"hello",
"meteor_accounts_loginServiceConfiguration",
"parttimejobs",
"postcodes",
"system.indexes",
"users"
]
meteor:PRIMARY>
我的包裹是
autopublish 1.0.3
ian:accounts-ui-bootstrap-3 1.2.69
insecure 1.0.3
meteor-platform 1.2.2
twbs:bootstrap 3.3.5
样本文件
meteor:PRIMARY> db.postcodes.findOne();
{
"_id" : ObjectId("559933dc4a8617644069fa5b"),
"postcode" : "AB10 1AB",
"latitude" : 57.149079,
"longitude" : -2.096964,
"county" : "",
"district" : "Aberdeen City",
"ward" : "George St/Harbour",
"constituency" : "Aberdeen North",
"loc" : [
-2.096964,
57.149079
]
}
假设您在应用程序启动时很早就尝试执行count()
如果您只需要计数,我建议您使用Meteor方法并异步获取计数,这样您就不必等待客户机订阅为170万个文档做好准备
下面是一个方法示例:
在服务器上:
Meteor.methods({
getSomeCollectionCount: function () {
return someCollection.find().count();
}
});
然后在客户机上:
Meteor.call('getSomeCollectionCount', function (error, result) {
if(!error) {
// add code to be run when count is ready
console.log('someCollection count: ', result);
}
});
另一种方法是使用pub-sub或iron-router-waiton,但如果您只是想获得数量,我不建议客户订阅所有170万份文档。正如许多人提到的,这是由于数据量大。删除自动发布并创建频道后,将返回预期结果
Meteor.publish('postCodesChannel', function(){
return PostCodes.find({postcode: {$regex: /B14 6B[DE]/}});
});
浏览器控制台现在返回适当的值
>PostCodes.find().count();
2
没有看到代码就无法知道。我已经用所有集合和模型更新了我的问题。js您是否删除了自动发布包?我还没有创建频道。因此,autopublish仍然存在。当客户端上还没有收集时,您可能会要求计数,您必须在等待收集就绪的路由中创建一些waitOn函数。protip:不要在每次页面加载时发送千兆字节的数据。