【问题标题】:Meteor publication sorting流星出版物排序
【发布时间】:2018-05-24 18:51:11
【问题描述】:

我有一个 Meteor-react 应用程序,其中包含一个集合,包含大量数据。我用分页显示数据。
在服务器端,我只是发布当前页面的数据。

所以,我在服务器端发布一些数据:

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 日起。 类型检查是在两边完成的。

【问题讨论】:

  • Counts.publish 与您的问题相关吗?
  • 它不应该是相关的,因为它只是计数。
  • 请记住,当您使用排序 + 限制时,使用不同的排序可能会极大地改变已发布的文档。如果您只想对客户端收到的文档进行排序,您应该在客户端而不是服务器端进行排序。
  • 感谢您的信息,但我需要在服务器端进行排序,并且还必须从服务器端限制它,因为性能更快。 (有更多的对象)我认为排序应该首先发生,然后限制它。不应该吗?

标签: mongodb reactjs meteor meteor-publications


【解决方案1】:

我能想到的两件事。

  1. 密切关注 {sort: options.sortOption, skip: skip, limit: displayPerPage } 顺序。据我所知,它按照您放置的顺序运行。所以它首先排序,然后跳过,然后限制。

  2. 在客户端和服务器上进行排序。当排序发生在服务器上并将其流式传输到客户端时,客户端拥有一个不保证订单的迷你 mongo 版本。因此,您还需要对客户端进行排序。

【讨论】:

  • 谢谢,1 - 我知道,这就是重点。我只想发布 displayPerPage 个文档。 2 - 我真的不明白为什么,但效果更好。不完美,但分类精度为 95%。 (“W”和“Z”之间有一些“D”,但效果比以前好得多。
    我很困惑。因为客户端应该只得到有限的数据。
    请问您有什么意见剩下的 5%?:D
  • 所以现在也在跟踪器中排序:let data = Animals.find({}, {sort: options.sortOption} ).fetch({});
  • 在不同的数据库上测试过,在两个数据库上都存在“W”和“Z”之间的“D”
猜你喜欢
  • 2017-10-15
  • 2014-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-02
  • 1970-01-01
  • 2023-03-15
  • 2017-01-01
相关资源
最近更新 更多