【问题标题】:Meteor JS : How to get latest set of data based on the date?Meteor JS:如何根据日期获取最新的数据集?
【发布时间】:2016-02-09 20:48:19
【问题描述】:

我有一个要求,我的数据库已经说了一些具有相同时间戳(最新)的记录,我想一次获取所有记录,我不想获取任何其他不属于该记录的数据标准,问题是我不知道时间戳,因为它从外部存储在数据库中。

如何仅获取流星中的最新数据集?我不能做 findOne,因为它只会带来 1 条最新记录,这在我的情况下是错误的。

Meteor.publish("collection1", function() {
  return Collection1.find({}, {sort:{dateTime: -1}});
});

我尝试执行上述代码,但它获取了所有记录,我认为它只是按 desc 排序。

【问题讨论】:

  • {sort:{dateTime: -1}, limit: 10}

标签: mongodb meteor


【解决方案1】:

使用 findOne() 方法获取最新的 dateTime 值,然后您可以将其用作 find() 查询:

// on the server
Meteor.publish("collection1", function() {
    var latest = Collection1.findOne({}, {"sort":{"dateTime": -1}}).dateTime;
    return Collection1.find({"dateTime": latest});
});

// on the client
Meteor.subscribe("collection1");
Collection1.find().fetch();

上述方法使您的 Meteor 应用程序可扩展客户端,因为客户端仅订阅您只需要的数据。这是因为 Meteor.publish 函数内的 return 语句只能返回具有最新 dateTime 值的文档。这样,无论服务器端数据库有多大,都可以避免浏览器内存过载。

【讨论】:

  • 在客户端(订阅)我应该返回 Collection1.find({}) ??或者我需要你提到的同样的东西?
  • @SharathBangera 我已经对如何在客户端订阅的答案添加了一些解释。
  • @chridam 这是如何工作的,当我使用限制时,比如获取最新的 50 条记录?
猜你喜欢
  • 2021-01-28
  • 1970-01-01
  • 2022-11-12
  • 2020-08-12
  • 1970-01-01
  • 2022-12-09
  • 2021-07-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多