restrict documents for mapreduce with mongoid

Posted by theBernd on Stack Overflow See other posts from Stack Overflow or by theBernd
Published on 2011-01-08T13:03:42Z Indexed on 2011/01/09 10:53 UTC
Read the original article Hit count: 337

Filed under:
|
|
|

I implemented the pearson product correlation via map / reduce / finalize. The missing part is to restrict the documents (representing users) to be processed via a filter query. For a simple query like

mapreduce(mapper, reducer, :finalize => finalizer, :query => { :name => 'Bernd' })

I get this to work.

But my filter criteria is a little bit more complicated: I have one set of preferences which need to have at least one common element and another set of preferences which may not have a common element. In a later step I also want to restrict this to documents (users) within a certain geographical distance.

Currently I have this code working in my map function, but I would prefer to separate this into either query params as supported by mongoid or a javascript function. All my attempts to solve this failed since the code is either ignored or raises an error.

I did a couple of tests. A regular find like

User.where(:name.in => ['Arno', 'Bernd', 'Claudia'])

works and returns

#<Mongoid::Criteria:0x00000101f0ea40 
  @selector={:name=>{"$in"=>["Arno", "Bernd", "Claudia"]}}, 
  @options={}, @klass=User, @documents=[]> 

Trying the same with mapreduce

User.collection.
     mapreduce(mapper, reducer, :finalize => finalizer, 
               :query => { :name.in => ['Arno', 'Bernd', 'Claudia'] })

fails with `serialize': keys must be strings or symbols (TypeError) in bson-1.1.5

The intermediate query parameter looks like this

:query=>{#<Mongoid::Criterion::Complex:0x00000101a209e8 @key=:name,
         @operator="in">=>["Arno", "Bernd", "Claudia"]}

and at least @operator looks a bit weird to me. I'm also uncertain if the class name can be omitted.

BTW - I'm using mongodb 1.6.5-x86_64, and the mongoid 2.0.0.beta.20, mongo 1.1.5 and bson 1.1.5 gems on MacOS.

What am I doing wrong?

Thanks in advance.

© Stack Overflow or respective owner

Related posts about query

Related posts about mongodb