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

  • Home
  • SEARCH
  • 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 5951593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:33:34+00:00 2026-05-22T17:33:34+00:00

I am using Rail 3 with Mongoid as my ODM. I have imported the

  • 0

I am using Rail 3 with Mongoid as my ODM.

I have imported the following documents into MongoDB:

{ "make" : "Make A", "model": "Model 1", "variant" : "Variant 1" }
{ "make" : "Make B", "model": "Model 3", "variant" : "Variant 1" }
{ "make" : "Make A", "model": "Model 2", "variant" : "Variant 2" }
{ "make" : "Make A", "model": "Model 2", "variant" : "Variant 1" }

The following code produces a nested hash of sorted distinct values:

@makes = Item.all.distinct(:make).sort

@models = {}
@makes.each do |make|
  @models[make] = Item.where(:make => make).distinct(:model).sort
end

@output = {}
@models.each_pair do |make, models|
  @output[make] = {}
  models.each do |model|
    @output[make][model] = Item.where(:make => make, :model => model).distinct(:variant).sort
  end
end

The resulting hash looks like this:

{
  "Make A" => {
    "Model 1" => ["Variant 1"],
    "Model 2" => ["Variant 1", "Variant 2"]
  },
  "Make B" => {
      "Model 3" => ["Variant 1"]
  }
}

This all works fine, but is very inefficient as it involves so many queries. Is there a better way of achieving this, perhaps by having MongoDB perform the aggregation?

  • 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-05-22T17:33:35+00:00Added an answer on May 22, 2026 at 5:33 pm

    I solved this using MongoDB’s MapReduce function with the following paramaters:

    map = function() { 
      emit( 1, { make: this.make, model: this.model, variant: this.variant } ); 
    }
    
    reduce = function(key, values) {
      var result = {};
      values.forEach(function(value) {
        if (!result[value.make]) result[value.make] = {};
        if (!result[value.make][value.model]) result[value.make][value.model] = [];
        result[value.make][value.model].push(value.variant);
      });
      return result;
    }
    

    This returns a single MongoDB result in the same format as the Ruby hash above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Devise in a rail app. I have two models : User
I have to model an association structure and the association is divided into divisions/subdivisions/sections
hi I am using following code to pop up Facebook application request dialog in
Using boost python I need create nested namespace. Assume I have following cpp class
Using delphi 2010, i am wondering if there someway to escape the following string
I am using aptana radrails empty rail project : scaffold Article titre:string body:text categorie_id:integer
Using android 2.3.3, I have a background Service which has a socket connection. There's
Is it possible to query unsaved changes using Rail's ActiveRecord or another similar approach?
So far I have been building my Rail app on a Mac OS X
Using the following function: // remove multiple, leading or trailing spaces function trim(s) {

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.