Meteor/mongo 获取一个月前创建的记录

Meteor/mongo get records created one month ago

如何从一个月前创建的 collection 中获取记录 moment.js return 所有记录的计数。

我的collection代码是:

Products.find() // where start and end date is one month ago and  products is the collection

这可能是一个愚蠢的问题,但是,由于我是从 rails 到 javascript/meteor,所以有点帮助就可以了。

示例文档

 {
        "_id": "xxx",
        "name": "xxx",
        "description": "xxx",
        "createdAt": "2015-04-18T04:40:00Z",
        "productType": "Flavoured Milk",
        "opt1": "Packaging",
        "opt2": "Weight",
        "supplier": "xxx",
        "brand": "xxx"
        "status": "active",
        "tags": ["xxx","xxx"],
        "updatedAt": "2015-04-18T04:40:00Z"
    }

您可以通过以下方式获取一个月前的Date对象:

moment().subtract(1, "months").toDate();

我假设您在 Products 集合上有字段 createdAt。因此,查找一个月前创建的所有产品的查询将是:

var oneMonthAgo = moment().subtract('months', 1).toDate();

Products.find({ createdAt: oneMonthAgo })

但请记住,上面的解决方案 return 只会记录一个月前 正好 创建的记录 精确到秒 .

乌钦纳,

Moment.js 2.8.0 中你可以这样做:

var comparison = moment().subtract(1, 'months').toDate();

Products.find({ createdAt: comparison });

根据文档:

Before version 2.8.0, the moment#subtract(String, Number) syntax was also supported. It has been deprecated in favor of moment#subtract(Number, String).