MomentJs 仅返回 Meteor 中上周内的日期
MomentJs only returning dates within the last week in Meteor
我在 Meteor 的集合中有大量日期。目前我只是 return 将它们限制为 10,但我需要 return 它们直到上周(7 天)。我的问题是每天没有相同数量的物品,所以我不能每天都拿相同的数量。我正在将 Meteor 与 moment js 一起使用。这是一些数据的示例和我用来 return 它的代码:
{
_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 字段。
我在 Meteor 的集合中有大量日期。目前我只是 return 将它们限制为 10,但我需要 return 它们直到上周(7 天)。我的问题是每天没有相同数量的物品,所以我不能每天都拿相同的数量。我正在将 Meteor 与 moment js 一起使用。这是一些数据的示例和我用来 return 它的代码:
{
_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 字段。