I have a simple dataset in a MongoDB (version 2.2.2):
{
"_id": "527d60865593622ba17643e6",
"siteid": "100",
"date": 1383940800,
"visits": 1
},
{
"_id": "527d60865593622ba17643e7",
"siteid": "200",
"date": 1383940801,
"visits": 1
},
{
"_id": "527d60865593622ba17643e8",
"siteid": "200",
"date": 1383940802,
"visits": 1
}
I have a Mongoid (version 3.0.1) collection where I'm trying to use the aggregate method:
query = {
"$project" => { _id: 0 },
"$match" => {"$and" => [{:date=>{"$gte"=>1383940800}}, {:date=>{"$lt"=>1384027200}}]},
"$group" => {"_id" => "$siteid", "visits" => {"$sum" => "$visits"}},
"$project" => { "siteid" => "$_id" }
}
result = @object.collection.aggregate(query)
The result that I get back is this:
[{"_id"=>200, "visits"=>2}, {"_id"=>100, "visits"=>1}]
What I'd like the result to be is this:
[{"siteid"=>200, "visits"=>2}, {"siteid"=>100, "visits"=>1}]
I thought the last $project operation would do this for me but it seems to be completely ignored. Any suggestions?