title: Mongodb Aggregation count array/set size
body: <p>Here's my problem:</p>
  
  <p>Model:</p>
  
  <blockquote>
    <p>{ application: "abc", date: Time.now, status: "1" user_id: [ id1, id2,
    id4] }</p>
    
    <p>{ application: "abc", date: Time.yesterday, status: "1", user_id: [
    id1, id3, id5] }</p>
    
    <p>{ application: "abc", date: Time.yesterday-1, status: "1", user_id: [
    id1, id3, id5] }</p>
  </blockquote>
  
  <p>I need to count the unique number of user_ids in a period of time.</p>
  
  <p>Expected result:</p>
  
  <blockquote>
    <p>{   application: "abc",   status: "1",   unique_id_count: 5 }</p>
  </blockquote>
  
  <p>I'm currently using the aggregation framework and counting the ids outside mongodb.</p>
  
  <blockquote>
    <p>{ $match: { application: "abc" } }, { $unwind: "$users" },  { $group:
    { _id: { status: "$status"}, 
               users: { $addToSet: "$users" } } }</p>
  </blockquote>
  
  <p>My arrays of users ids are very large, so I have to iterate the dates or I'll get the maximum document limit (16mb).</p>
  
  <p>I could also $group by</p>
  
  <blockquote>
    <p>{ year: { $year: "$date" }, month: { $month: "$date" }, day: {
    $dayOfMonth: "$date" }</p>
  </blockquote>
  
  <p>but I also get the document size limitation.</p>
  
  <p>Is it possible to count the set size in mongodb?</p>
  
  <p>thanks</p>