Search code examples
ruby-on-railsmongodbmongomapper

Querying mongodb/mongomapper for data across associations


Im using mongomapper to back a rails app with mongodb and need to query across associations for a given dataset.

for eg.

class User
  include Mongomapper::Document

  key :age, Integer
  many :talents
end 
class Talent
  include Mongomapper::Document

  key :name, String
end

Now if I want to search for a user who is age 31, I can easily do

User.find_by_age(31).

but how do I search for a user with age 31 who has Juggling as a talent ?? Something like this:

User.find_by_age_and_talent(31, "juggling")

Doesnt have to be through mongomapper...could be a direct mongodb query.


Solution

  • Note that you have typos in your models, Mongomapper should be MongoMapper, catch this to save yourself some puzzlement.

    Your models specify separate documents, and thus your search criteria are in separate documents. So you cannot specify a single query that spans documents, that's the equivalent of a join, and there's no join in MongoDB. So as written, you are stuck with exhaustive secondary querying of a primary results set.

    However, if you modify Talent to be an embedded document, then you can do a single query in MongoDB, thus taking advantage of the beauty of MongoDB for both storage and querying, without the join overhead required by SQL.

    To run either version, just change the include in the following model to be either MongoMapper::Document or MongoMapper::EmbeddedDocument. The test file supports both without modification.

    class Talent
      #include MongoMapper::Document
      include MongoMapper::EmbeddedDocument
    
      key :name, String
    end
    

    test/unit/user_test.rb

    require 'test_helper'
    
    class UserTest < ActiveSupport::TestCase
      def setup
        User.delete_all
        Talent.delete_all if Talent.respond_to?(:delete_all)
      end
    
      test "user talent" do
        if Talent.respond_to?(:create)
          puts "Talent as separate document, not embedded"
          User.create(age: 31, talents: [Talent.create(name: 'juggling')])
          User.create(age: 31, talents: [Talent.create(name: 'singing')])
          User.create(age: 25, talents: [Talent.create(name: 'juggling'), Talent.create(name: 'dancing')])
          assert_equal(3, User.count)
          talent = 'juggling'
          users = User.where(age: 31).select{|user| Talent.where(name: talent).where(user_id: user._id).first}
          assert_equal(1, users.size)
          p users
        else
          puts "Talent as embedded document"
          User.create(age: 31, talents: [Talent.new(name: 'juggling')])
          User.create(age: 31, talents: [Talent.new(name: 'singing')])
          User.create(age: 25, talents: [Talent.new(name: 'juggling'), Talent.new(name: 'dancing')])
          assert_equal(3, User.count)
          talent = 'juggling'
          users = User.where(age: 31).where('talents.name' => talent).to_a
          assert_equal(1, users.size)
          p users
        end
      end
    end