I have User documents in a collection that have this structure:
{ "_id" : ObjectId( "4fb54ef46d93b33b21003951" ),
"activities" : [
{ "id" : ObjectId( "4fd66f9001e7fe9f03000065" ),
"type" : "checkin",
"date_time_created" : Date( 1339453328000 )},
{ "date_time_created" : Date( 1337351732000 ),
"date_time_updated" : Date( 1337351952635 ),
"id" : ObjectId( "4fb65e346d93b3fe77000000" )}
]
}
I can easily query these documents based on date:
User.where(
:activities => {
'$elemMatch' => {
:date_time_created => { '$gte' => start_date, '$lt' => end_date }
}
}
).length
According to logs:
MOPED: 127.0.0.1:27017 COMMAND database=db command={:count=>"users", :query=>{"activities"=>{"$elemMatch"=>{"date_time_created"=>{"$gte"=>2012-05-10 00:00:00 UTC, "$lt"=>2012-07-12 00:00:00 UTC}}}}} (0.5260ms)
I get the results I need this way.
However, when I'm trying to use the new aggregate function and $match based on the same criteria:
User.collection.aggregate( [
{ "$match" => {
:activities => {
'$elemMatch' => {
:date_time_created => { '$gte' => start_date, '$lt' => end_date }
}
}
} }
]).length
According to logs:
MOPED: 127.0.0.1:27017 COMMAND database=db command={:aggregate=>"users", :pipeline=>[{"$match"=>{:activities=>{"$elemMatch"=>{"date_time_created"=>{"$gte"=>Thu, 10 May 2012, "$lt"=>Thu, 12 Jul 2012}}}}}]} (0.6049ms)
"start_date" and "end_date" are Ruby Date objects and are essentially the same in both queries. However, when I look at the logs they are changed into different formats. When I try to force the format with something like start_date.strftime("%Y-%m-%d") it still doesn't work.
There are other functions in the aggregate pipeline, but I took them out and I still get the error.
How can I get the aggregate function working to match on Dates?
The following test uses the aggregation framework to approximate the logical equivalent of your query.
First $unwind the array, $match, then $group using $addToSet in case there are multiple array elements that match. Note that this is a lossy process, but this is acceptable if you only need the count or the ids.
Hope that this helps.
test/unit/user_test.rb
require 'test_helper'
class UserTest < ActiveSupport::TestCase
def setup
User.delete_all
end
test "user aggregate" do
User.create(
activities: [
{ id: Moped::BSON::ObjectId("4fd66f9001e7fe9f03000065"),
type: "checkin",
date_time_created: Time.at(1339453328000) },
{ date_time_created: Time.at(1337351732000),
date_time_updated: Time.at(1337351952635),
id: Moped::BSON::ObjectId("4fb65e346d93b3fe77000000") }
]
)
start_date = Time.at(1339453328000)
end_date = start_date + 24*60*60
assert_equal(1, User.where(
:activities => {
'$elemMatch' => {
:date_time_created => { '$gte' => start_date, '$lt' => end_date }
}
}
).length)
assert_equal(1, User.collection.aggregate( [
{ '$unwind' => '$activities' },
{ '$match' => { '$and' => [
{ 'activities.date_time_created' => { '$gte' => start_date } },
{ 'activities.date_time_created' => { '$lt' => end_date } }
] } },
{ '$group' => { '_id' => '$_id', 'activities' => { '$addToSet' => '$activities' } } }
] ).length)
end
end