Querying Group by and Count in MongoMapper

392 views
Skip to first unread message

sameera

unread,
Sep 11, 2010, 11:59:57 PM9/11/10
to MongoMapper
db.states.group({cond: {"action_id":"4c8210a3d8c2770ab4000006"},key:
{ date: true},initial: {count: 0},reduce: function(doc,out){out.count+
+;} })

when run above query at the shell it will give correct out put to me,
as following..

[
{
"date" : "2010-09-11",
"count" : 8
},
{
"date" : "2010-09-10",
"count" : 3
},
{
"date" : "2010-09-12",
"count" : 1
},
{
"date" : "2010-09-13",
"count" : 6
}
]

But I don't have idea how to use this query at Model to to get correct
out put..

Thanks,
NS.

Fabrizio Regini

unread,
Sep 12, 2010, 8:02:27 AM9/12/10
to mongo...@googlegroups.com
I'd run a map-reduce, group does not run on sharded environments, you would get into problems if you ever scale your database.

I'd run something like:
opts = { your filter conditions here }
map_function = %Q( function() {
emit( this.date, 1);
} )

reduce_function = %Q( function(key, values) {
var total = 0;
for ( var i=0; i<values.length; i++ ) {
total += values[i];
}
return total;
})


MyModel.collection.map_reduce( map_function, reduce_function, opts )

You'll get a list of documents where _id is date, and value is the count.
Nothing MM related, iterate over it as defined in Mongo Ruby Driver documentation.

> --
> You received this message because you are subscribed to the Google
> Groups "MongoMapper" group.
> For more options, visit this group at
> http://groups.google.com/group/mongomapper?hl=en?hl=en

Andy Rong

unread,
Apr 15, 2013, 9:44:13 PM4/15/13
to mongo...@googlegroups.com
Hi joseph,

Thanks for your reply. I haven't used something like 'last' query, just copy the previous m-r code snippet. I don't know why. 
Anyway, I sovled the problem in other approach.   

Bryan Mulvihill

unread,
Aug 21, 2013, 7:41:15 PM8/21/13
to mongo...@googlegroups.com
I get the same undefined method last when I am trying to run the map reduce query above using mongo mapper. Anyone else have this problem, or know a solution to it?

Joseph Caudle

unread,
Aug 21, 2013, 9:54:00 PM8/21/13
to mongo...@googlegroups.com
Bryan,

Could you provide a code snippet (maybe a gist) and some more context? I don't really know what you're facing.

Best,
Joseph

On Aug 21, 2013, at 7:41 PM, Bryan Mulvihill <mulvihi...@gmail.com> wrote:

> I get the same undefined method last when I am trying to run the map reduce query above using mongo mapper. Anyone else have this problem, or know a solution to it?
>
> --
> --
> You received this message because you are subscribed to the Google
> Groups "MongoMapper" group.
> For more options, visit this group at
> http://groups.google.com/group/mongomapper?hl=en?hl=en
> ---
> You received this message because you are subscribed to the Google Groups "MongoMapper" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to mongomapper...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Bryan Mulvihill

unread,
Aug 22, 2013, 3:01:20 PM8/22/13
to mongo...@googlegroups.com
Well the issue was I would try any map and reduce query and receive the error:  "undefined method `last' for nil:NilClass"
This was using MongoMapper, or just the Ruby Driver.

I ended up downgrading to an older version of bson (1.5.2) and now all of my map reduce queries are executing.  
I had been using 1.9.2, so I am wondering if their is a bug with that particular driver?

Joseph Caudle

unread,
Aug 22, 2013, 8:21:10 PM8/22/13
to mongo...@googlegroups.com
It definitely sounds like a bug in the driver. Do you have a backtrace available or have you seen any relevant bug reports on the driver?
Reply all
Reply to author
Forward
0 new messages