Last Updated: April 26, 2021
·
2.976K
· Alexandr K

Mongoid ModelName.collection.aggregate method with $match by dates.

Example:

Stat.collection.aggregate({
    '$match' => {
      '$and' => [
        { 'day' => { '$gte' => from.to_date.mongoize } },
        { 'day' => { '$lte' => to.to_date.mongoize } }
      ]
    },
  })

When are you using aggregate function it will use the method from the moped library. This library doesn't know anything about your params and you need to be sure that you are passing the right params.

Lets you have 'field :day, type: Date' you should use mongoize mongoid extension to convert date to the right view of the date.