MongoMapper $or and Set keys

Found an interesting issue (?) with $or and mongomapper — and a work-around.

Given (lots omitted):

class Patient
  include MongoMapper::Document
  ...
  key :count_public_encounters, Integer, :default => 0
  # Provide maps to the related "doctor_num(s)" unique IDs.
  key :doctor_num_list, Set, :index => true
  # ...and related "group_nums(s)"
  key :group_num_list, Set, :index => true
  ...
end

In testing, the following basic syntax worked for an AND condition to find public patient count for doctors in a given list:

n = by_docs = Patient.where( :count_public_encounters.gt =>0, :doctor_num_list.in => doc_list).count

Since patients could be associated with a doctor, a group, or both, I wanted to find public patient counts where the group number matched, OR the doctor numbers were in the list.

n = Patient.where(:count_public_encounters.gt => 0,
        :$or =>[{:group_num_list.in => [grp.group_num]},
                {:doctor_num_list.in =>[doc_list]}]).count

After all, this syntax for array query works as a solo part of an AND query. But no luck 🙁

Turns out, the following syntax worked:

n = Patient.where(:count_public_encounters.gt => 0,
        :$or => [{'group_num_list' => { '$in' =>[grp.group_num]} },
                {'doctor_num_list'  => { '$in' =>doc_list}}]).count

Not gonna ask why…

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.