mongodb - 流星出版物排序
问题描述
我有一个 Meteor-react 应用程序,其中包含一个包含大量数据的集合。我正在使用pagination显示数据。
在服务器端,我只是发布当前页面的数据。
所以,我在服务器端发布了一些数据:
Meteor.publish('animals', function(currPage,displayPerPage, options) {
const userId = this.userId;
if (userId) {
const currentUser = Meteor.users.findOne({ _id: userId });
let skip = (currPage - 1) * displayPerPage;
if (displayPerPage > 0) {
Counts.publish(this, 'count-animals', Animals.find(
{$and: [
// Counter Query
}
), {fastCount: true});
return Animals.find(
{$and: [
// Data query
]}, {sort: options.sortOption, skip: skip, limit: displayPerPage });
} else {
Counts.publish(this, 'count-animals', 0);
return [];
}
}
});
在客户端,我正在使用跟踪器:
export default AnimalsContainer = withTracker(({subscriptionName, subscriptionFun, options, counterName}) => {
let displayPerPage = Session.get("displayPerPage");
let currPage = Session.get("currPage");
let paginationSub = Meteor.subscribe(subscriptionName, currPage, displayPerPage, options );
let countAnimals = Counts.get(counterName);
let data = Animals.find({}).fetch({});
// console.log(data);
return {
// data: data,
data: data.length <= displayPerPage ? data : data.slice(0, displayPerPage),
countAnimals: countAnimals,
}
})(Animals);
问题是:
当我尝试在客户端修改排序选项时,服务器不是从第一个数据排序(跳过第一个数据)。有时从 20 日起有时从 10 日起。类型检查在两侧进行。
解决方案
我能想到两件事。
注意 {sort: options.sortOption, skip: skip, limit: displayPerPage } 顺序。据我所知,它按照您放置的顺序运行。所以它首先排序,然后跳过,然后限制。
在客户端和服务器上进行排序。当排序发生在服务器上并将其流式传输到客户端时,客户端拥有一个不保证订单的迷你 mongo 版本。因此,您还需要对客户端进行排序。
推荐阅读
- c# - 如何在名称文件中搜索包含 C#
- javascript - React Big Calendar - 自定义视图中的自定义道具
- angular - 角度重定向到模块
- php - Doctrine 自定义 MariaDB 平台
- r - 根据 r 中的天气每小时数据计算霜冻变化天数(天数)
- python - 从日期 4.5.6 中提取日、月和年
- python - 临时设置最大 col 宽度并保持 pandas 样式 - jupyter
- c# - NAudio 改变两个不同 WaveOutEvents 的音量而不影响另一个
- ios - 如何快速击中多个 CAShapeLayer?
- c++ - 错误 E0028 表达式必须有一个常量值