Query a deeply nested mongo

70 views Asked by At

Assume I have a Mongo database with the data organized as follows (and that I have no control over the data structure itself)

    {
    "id": 1,
     "data": {
       "y": {
         "2015": {
           "m": {
             "6": {
               "d": {
                 "28": {
                   "location": {
                     "country": {
                       "United Kingdom": {
                         "t": 50
                       },
                       "Iceland": {
                         "t": 4
                       }
                     }
                   }
                 }
               }
             }
           }
         }
       }
      }
    }

As we can see above, the m stands for month and so on. Let's say I want to print all countries from a particular month with id as something. So in this case, something like

    function(id,month):
            ...
            mongo code
            ...
    end      
    # output "United Kingdom","Iceland"

What's the best way of doing that? I'm using Mongoid on Ruby on Rails so using that would be alright as well though I'm quite curious how this in accomplished in Rails.

0

There are 0 answers