3

I'm having trouble with the MongoDB ruby driver (via mongoid) using aggregation.

I would like to match against a date using comparison operators.

match = { '$match' => { 'created_at' => { '$gte' => DateTime.parse('2012-08-01') } } }
group = { '$group' => { '_id' => 'foo' } }
MyModel.collection.aggregate([match, group])

I don't know what to put in the first line for the date. The code as written above will throw me an undefined method __bson_dump__ for DateTime exception. Using a string doesn't seem to work, either.

Any suggestions are welcome. MongoID's built in methods give me what I need for the selection but not for the grouping.

David T
  • 765
  • 6
  • 18

2 Answers2

2

Figured it out, use a Time object instead of DateTime

match = { '$match' => { 'created_at' => { '$gte' => Time.parse('2012-08-01') } } }
David T
  • 765
  • 6
  • 18
  • 1
    It is not working, It convert the date to String, which can not be compared to DateTime fields in Mongo – panshul Jul 15 '19 at 17:54
-2

Try this.

h = { '$match' => { 'created_at' => { '$gte' => new Date("2012/08/01") } } }
usha
  • 28,973
  • 5
  • 72
  • 93