我有一个angular-meteor应用程序,它需要使用angularUtils.directives.dirPagination的53,296个文档集合中的材料md-autocomplete,但这些数据会让我的浏览器挂起。
我将通过以下方式发布该集合:
Meteor.publish('city', function (options, searchString) {
var where = {
'city_name': {
'$regex': '.*' + (searchString || '') + '.*' ,
'$options': 'i'
}
};
return City.find(where, options);
});我的订阅地址是:
subscriptions: function () {
Meteor.subscribe('city');
this.register('city', Meteor.subscribe('city'));
}并在控制器上进行分页:
$scope.currentPage = 1;
$scope.pageSize = 100;
$scope.sort = {city_name_sort : 1};
$scope.orderProperty = '1';
$scope.helpers({
city: function(){
return City.find({});
}
});但是它需要很长的时间来加载,并且它会使chrome停止工作。
发布于 2017-08-29 23:45:01
您已经完成了大部分服务器端搜索,因为您的搜索是在订阅中运行的。您应该确保在mongo中对city_name字段进行了索引!您应该只返回该字段,以最大限度地减少数据传输。您还可以简化您的正则表达式。
Meteor.publish('city', function (searchString) {
const re = new RegExp(searchString,'i');
const where = { city_name: { $regex: re }};
return City.find(where, {sort: {city_name: 1}, fields: {city_name: 1}});
});我发现服务器端自动补全的帮助是:
.find()就会运行一次(而不是仅仅查询{})。这是一个很好的做法,因为客户端集合是该集合上所有订阅的联合,其中可能存在您不想列出的文档。最后,我不知道你为什么要在这里订阅两次:
subscriptions: function () {
Meteor.subscribe('city');
this.register('city', Meteor.subscribe('city'));
}这些Meteor.subscribe('city')调用中只有一个是必需的。
https://stackoverflow.com/questions/45896031
复制相似问题