質問

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.

役に立ちましたか?

解決

Figured it out, use a Time object instead of DateTime

match = { '$match' => { 'created_at' => { '$gte' => Time.parse('2012-08-01') } } }

他のヒント

Try this.

h = { '$match' => { 'created_at' => { '$gte' => new Date("2012/08/01") } } }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top