我在 Meteor 的集合中有大量日期。目前我只是以 10 件为限返回,但我需要在上周(7 天)内返回。我的问题是每天没有相同数量的元素,所以我不能每天都拿相同的数量。我正在将 Meteor 与 moment js 一起使用。这是一些数据的示例以及我用来返回它的代码:
{
_id: "a68JFTrCFabQe5qQ2",
createdAt: Tue Dec 15 2015 09:32:36 GMT+1100 (AUS Eastern Summer Time),
user: "7uXThqXFkjkMpDrcb"
}
//This data gets to the client with the following:
getDay: function(day){
return Time.find({today: day}, {limit: 10}).fetch();
}
//Instead of limiting it by 10 I need items from the last 7 days only.
谢谢!
最佳答案
您需要搜索 createAt 大于今天 - 7 天的记录。假设您在集合中创建这些对象时使用普通的 javascript Date() 对象,您将使用以下代码获取上周的所有记录:
Time.find({
createdAt: {
$gte: new moment().subtract(1, 'week').toDate(),
$lte: new Date()
}
});
如果您正在使用 moment.如果不是,只需使用常规 Date() 并减去一周并将其放入 $gt 字段。
关于javascript - MomentJs 仅返回 Meteor 中上周内的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34278272/