title: Row count in mongoDB
body: <p>Employee document is there with view sequence. But for pagination there are few more logic is present. So need to do bucketing in mongo db. Below is sample employee collection mentioned</p>
  
  <pre><code>{employee:1, view_seq : 1, [{Subdept_name: Subdept1},{Subdept_name: Subdept2}]},
  {employee:1,view_seq :  2, [{Subdept_name: Subdept3},{Subdept_name: Subdept4}]},
  {employee:1, view_seq : 3, [{Subdept_name: Subdept5},{Subdept_name: Subdept6}]},
  {employee:2, view_seq : 1, [{Subdept_name: Subdept1},{Subdept_name: Subdept2}]},
  {employee:2, view_seq : 2, [{Subdept_name: Subdept3},{Subdept_name: Subdept4}]},
  {employee:2, view_seq : 3, [{Subdept_name: Subdept5},{Subdept_name: Subdept6}]},
  {employee:3, view_seq : 1, [{Subdept_name: Subdept1},{Subdept_name: Subdept2}]},
  {employee:3, view_seq : 2, [{Subdept_name: Subdept3},{Subdept_name: Subdept4}]},
  {employee:3, view_seq : 3, [{Subdept_name: Subdept5},{Subdept_name: Subdept6}]},
  </code></pre>
  
  <p>I want output like</p>
  
  <pre><code>{employee:1, view_seq : 1, [{Subdept_name: Subdept1},{Subdept_name: Subdept2}]},"values" : { "emp_seq" : 1, "cnt" : 1 } }
  {employee:1,view_seq :  2, [{Subdept_name: Subdept3},{Subdept_name: Subdept4}]},], "values" : { "emp_seq" : 1, "cnt" : 1 } }
  {employee:1, view_seq : 3, [{Subdept_name: Subdept5},{Subdept_name: Subdept6}]}, "emp_seq" : 1, "cnt" : 1 } }
  {employee:2, view_seq : 1, [{Subdept_name: Subdept1},{Subdept_name: Subdept2}]}, "values" : { "emp_seq" : 2, "cnt" : 1 } }
  {employee:2, view_seq : 2, [{Subdept_name: Subdept3},{Subdept_name: Subdept4}]}, "values" : { "emp_seq" : 2, "cnt" : 1 } }
  {employee:2, view_seq : 3, [{Subdept_name: Subdept5},{Subdept_name: Subdept6}]}, "student_seq" : 2, "cnt" : 1 } }
  {employee:3, view_seq : 1, [{Subdept_name: Subdept1},{Subdept_name: Subdept2}]},"values" : { "emp_seq" : 1, "cnt" : 2 } }
  {employee:3, view_seq : 2, [{Subdept_name: Subdept3},{Subdept_name: Subdept4}]},], "values" : { "emp_seq" : 1, "cnt" : 2 } }
  {employee:3, view_seq : 3, [{Subdept_name: Subdept5}]},"values" : {"emp_seq" : 1, "cnt" : 2 } }
  </code></pre>
  
  <p>This is required for pagination. The logic is, lets say 2 employees per page, i.e Employee <code>#1</code> and <code>#2</code>. But only <code>Subdept1</code> and <code>Subdept2</code> for these 2 employees in first page. <code>Subdept3</code> and <code>Subdept4</code> for <code>Employee#1</code> &amp; <code>Employee#2</code> in page 2. And <code>Subdept5</code> and <code>Subdept6</code> for the same employees in 3rd page. Again, in 4th page, <code>Subdept1</code> and <code>Subdept2</code> list for Employee <code>#3</code> and <code>#4</code> will be shown. This can be achieved by the formula</p>
  
  <pre><code>3*(CNT -1) + view_seq
  </code></pre>
  
  <p>I have done this in below process - </p>
  
  <pre><code>db.employee.aggregate([
     {
        $project: {
       _id :0,
       employee: 1,view_seq : 1,Subdept_name : 1, 
       values: {    
           emp_seq :{
              $let: {
                 vars: {
                     count : {$mod: ["$employee",2]} // 2 is per page emp no.
                      },
                 in:  { $cond: { if: { $eq : ["$$count",0] }, then: 2 , 
                          else: "$$count" }  } 
                  }
              },
      cnt : { $cond: { if: { $lte : ["$employee",2] }, then: 1, 
                          else:  { $divide: ["$employee",2]}} },// 2 is per page emp no.
          }
        }
     }
  ] )
  </code></pre>
  
  <p>But the problem is an employee can't be sequential in DB as emp can deleted from DB. Is there any other way by which I can count row number to achieve the same?</p>