Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7971131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:34:06+00:00 2026-06-04T07:34:06+00:00

Im using mongomapper to back a rails app with mongodb and need to query

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:34:07+00:00Added an answer on June 4, 2026 at 7:34 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a Rails application using MongoDB as the back-end and MongoMapper as the
I am using mongomapper throughotu my rails 3 app but am using authlogic and
I am using Rails with MongoDB and MongoMapper. My problem is that I have
I'm enjoying the pretty amazing MongoDB in Rails by using Mongomapper but I am
I'm trying to do a 'within' query using mongomapper. When I execute the command
I've just begun creating a rails application using mongomapper for my models. I'm wondering
I am developing a facebook application in Ruby on Rails using MongoDB as database.
I'm using MongoDB and Mongo Mapper and need to find an embedded document inside
I'm using Rails 3.0.1, HAML 0.3.22, and Mongrel 1.1.5 (and MongoMapper not AR). I'm
Using Rails 3.2.0.rc2 and ruby 1.9.3p0 In app/views/requests/_form.html.erb I have the following code for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.