在 rails mongodb 中查找特定月份的数据

find data for a particular month in rails mongodb

我正在尝试查询在特定月份创建的数据。

@events = Event.aggregates([
                               {
                                   '$project': {
                                       _id: 1,
                                       created_at: 1,
                                       'month': {'$month': '$created_at'}
                                   },
                               },
                               {month: {'$match': 05}}
                           ])

聚合没有给我任何结果。

我在邮递员那里收到回复,

{
    "count": 0,
    "sum": null,
    "avg": null,
    "min": null,
    "max": null
}

我个人更喜欢 collection.aggregate 而不是 aggregates。其次,$match 管道是错误的。最后,即使它在 ruby 中有效,也不要写 05,因为某些语言可能会将其解释为八进制而不是十进制。

@events = Event.collection.aggregate([
                               {
                                   '$project': {
                                       _id: 1,
                                       created_at: 1,
                                       'month': {'$month': '$created_at'}
                                   },
                               },
                               {"$match" => {"month" => {'$eq': 5}}}
                           ]).to_a