title: mongodb performance issue when fetching facet count and data
body: <p>I'm new using MongoDB.<br>
  I have a collection of documents like this:</p>
  
  <pre><code>{
      "_id" : ObjectId("58204f60536d1a27736d512b"),
      "last_name" : "Vinaykumar",
      "university_name" : "Osmania University",
      "job_483" : 1,
      "xiith_mark" : 0,
      "id" : "3305775",
      "first_name" : "V",
      "course_name" : "Diploma",
      "institute_name_string" : "Govt.Polytechnic,Kothagudem",
      "profile_percentage" : 60,
      "xiith_mark_type" : "Percentage",
      "xth_mark_type" : "Percentage",
      "date_of_birth" : "11-March-1995",
      "xth_mark" : 0,
      "last_login" : 1379565790,
      "percentage" : 76,
      "job_details" : [
      {
          "status" : NumberLong(0),
          "applied_date" : NumberLong(1476703354),
          "contact_viwed_status" : 0,
          "label_name" : [ ],
          "questionnaire_status" : 0,
          "batch_id" : null,
          "owner_type" : "searches",
          "call_letter" : null,
          "owner_id" : NumberLong(465)
      },
      {
          "status" : NumberLong(0),
          "applied_date" : NumberLong(1477051963),
          "contact_viwed_status" : 0,
          "label_name" : [ ],
          "questionnaire_status" : 0,
          "batch_id" : null,
          "owner_type" : "searches",
          "call_letter" : null,
          "owner_id" : NumberLong(482)
      },
      {
          "status" : NumberLong(0),
          "applied_date" : NumberLong(1477052973),
          "contact_viwed_status" : 0,
          "label_name" : [ ],
          "questionnaire_status" : 0,
          "batch_id" : null,
          "owner_type" : "searches",
          "call_letter" : null,
          "owner_id" : NumberLong(483)
     }
     ],
     "branch_name" : "Electrical &amp; Electronics",
     "candidate_state_name" : "Andhra Pradesh",
     "candidate_city_name_string" : "Andhra Pradesh-other",
     "10" : 10,
     "12" : 12,
     "gender" : "Male",
     "fw_id" : "FW15651132",
     "cgpa" : 0,
     "picture_path" : "",
     "hq_passout_year" : 2013
  }
  </code></pre>
  
  <p>I need to find data from this db with facet count .<br>
  <strong>Need facet count for following fields</strong>  </p>
  
  <ul>
  <li>job_details.status ,  job_details.label_name, 
  job_details.contact_viwed_status,   candidate_city_name_string,<br>
  course_name,   hq_passout_year,   branch_name , 
  candidate_sublocation_name_string ,  skill</li>
  </ul>
  
  <p>And total matching count and data with limit </p>
  
  <p>I did separate query for each facet and one query for total count and one query for data<br>
  Total   9+1+1=11 querys</p>
  
  <p><strong>Query's are</strong></p>
  
  <p>facet query's for job_details.status and job_details.label_name and job_details.contact_viwed_status did like this  </p>
  
  <pre><code>db.Response.aggregate([
      {"$match":{"$and":[{"job_details.owner_id" : 428},    
      {"job_details.owner_type" : 'searches'}]}}, 
      {"$unwind": "$job_details" }, 
      {"$group": {"_id":"$job_details.status","count": {"$sum": 1 }} }
      ])
  </code></pre>
  
  <p>other 6 facet query's like this</p>
  
  <pre><code>db.Response.aggregate([ {"$and":[{"job_details.owner_id" : 428},{"job_details.owner_type" : 'searches'}]},
                  {"$group": {"_id":"$candidate_city_name_string","count": {"$sum": 1 }}}] )  
  </code></pre>
  
  <p><strong>query for collecting data</strong></p>
  
  <pre><code> db.Response.aggregate([
                {"$and":[{"job_details.owner_id" : 428}{"job_details.owner_type" : 'searches'}]},
                {"$limit":25},``
                { "$skip":0} ,
                {"$unwind":"$job_details"}])
  </code></pre>
  
  <p><strong>query for collecting total count</strong></p>
  
  <pre><code>db.Response.find({"$and":[{"job_details.owner_id" : 428},{"job_details.owner_type" : 'searches'}]}).count()
  </code></pre>
  
  <p><strong>Total 3+9+1+1=11 Querys</strong>
    So performance is very very low . Is possible to make this 11 query to single query or how can i improve the performance.<br>
  please help. </p>