MongoId perform day / month specific queries on the date

679 views Asked by At

I am using MongoId 3,Rails 3.2 ,Ruby 1.9.3.

I wanted to query only part of a date, such as the day, week or month.

So for example, let's say we need to find all users that signed up on a Wednesday OR get all users whose birthday is on 25 day Or get all users who born in April month(not specific to particular year here) etc..

In Mysql we have select * from users where extract(dow from created_at) = 3;

But how can we perform this using MongoId.

2

There are 2 answers

1
Arthur Neves On

Here is a pretty handy cookbook on querying data ranges on Mongodb: http://cookbook.mongodb.org/patterns/date_range/ . and you can just create the query almost in the same way on Mongoid, using .where

0
sfritter On

I think the aggregation framework is a better fit for this. This question discusses the use of date operators in aggregation.

I've not had much experience with Mongoid, but it seems like it should support aggregation just fine, as long as you are using a recent version.