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

时间:2017-07-11 07:25:18

标签: ruby-on-rails mongodb mongoid aggregate

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

@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
}

1 个答案:

答案 0 :(得分:0)

我个人更喜欢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