title: Combining results of two tables in mongoid/mongo
body: <p>Hi guys what would be the best way to combine results of two mongoid queries. </p>
  
  <p>My issue is that I would like to know active users, A user can send a letter and a notification, both are separate table and a user if he sends either the letter or the notification is considered active. What I want to know is how many active users were there per month.</p>
  
  <p>right now what I can think of is doing this</p>
  
  <pre><code>Letter.collection.aggregate([
                         { '$match': {}.merge(opts) },
                         { '$sort': { 'created_at': 1 } },
                         {
                           '$group': {
                             _id: '$customer_id',
                             first_notif_sent: {
                               '$first': {
                                 'day': { '$dayOfMonth': '$created_at' },
                                 'month': { '$month': '$created_at' },
                                 'year': { '$year': '$created_at' }
                               }
                             }
                           }
                         }])
  Notification.collection.aggregate([
                         { '$match': {}.merge(opts) },
                         { '$sort': { 'created_at': 1 } },
                         {
                           '$group': {
                             _id: '$customer_id',
                             first_notif_sent: {
                               '$first': {
                                 'day': { '$dayOfMonth': '$created_at' },
                                 'month': { '$month': '$created_at' },
                                 'year': { '$year': '$created_at' }
                               }
                             }
                           }
                         }])
  </code></pre>
  
  <p>What I am looking for is to get the minimum of the dates and then combine the results and get the count. Right now I can get the results and loop over each of them and create a new list. But I wanted to know if there is a way to do it in mongo directly.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>For letters</p>
  
  <pre><code>  def self.get_active(tenant_id)
      map = %{
        function() {
          emit(this.customer_id, new Date(this.created_at))
        }
      }
  
      reduce = %{
        function(key, values) {
          return new Date(Math.min.apply(null, values))
        }
      }
      where(tenant_id: tenant_id).map_reduce(map, reduce).out(reduce: "#{tenant_id}_letter_notification")
    end
  </code></pre>
  
  <p>Notifications</p>
  
  <pre><code>def self.get_active(tenant_id)
      map = %{
        function() {
          emit(this.customer_id, new Date(this.updated_at))
        }
      }
  
      reduce = %{
        function(key, values) {
          return new Date(Math.min.apply(null, values))
        }
      }
      where(tenant_id: tenant_id, transferred: true).map_reduce(map, reduce).out(reduce: "#{tenant_id}_outgoing_letter_standing_order_balance")
    end
  </code></pre>
  
  <p>This is what I am thinking of going with, one of the reason is that, lookup does not work with my version of mongo.</p>