title: In MongoDB, how do I search in an array of sub-documents?
body: <p>I have a survey document in mongodb, each survey have <code>surveyRefId</code> for unique identification. I am not able to understand how to find sub-documents having <strong>questionType = hard</strong> in documents whose <strong>surveyRefid = 377 or 360</strong>.</p>
  
  <p>Here is a sample document:</p>
  
  <pre><code>{
    "json": {
      "surveyRefId": 377,
      "surveyName": "survey on sociology",
      "questionsVoList": [
        {
          "questionId": "556708425215763c64b8af3d",
          "questionText": "question no 1",
          "questionTitle": "",
          "questionType": "hard",
          "preQuestion": true,
          "questionOptions": [
            {
              "questionRefId": 0,
              "optionType": "RADIOBUTTON",
              "isanswer": true,
              "optionText": "ch1"
            }
          ]
        },
        {
          "questionId": "556708425215763c64b8af3d",
          "questionText": "question no 2",
          "questionTitle": "",
          "questionType": "simple",
          "question": true,
          "questionOptions": [
            {
              "questionRefId": 0,
              "optionType": "RADIOBUTTON",
              "isanswer": true,
              "optionText": "ch1"
            }
          ],
        },
        {
         "questionId": "556708425215763c64b8af3d",
          "questionText": "question no 3",
          "questionTitle": "",
          "questionType": "hard",
          "questionOptions": [
            {
              "questionRefId": 0,
              "optionType": "RADIOBUTTON",
              "isanswer": true,
              "optionText": "ch1"
            },
            {
              "questionRefId": 0,
              "optionType": "RADIOBUTTON",
              "isanswer": false,
              "optionText": "ch2"
            }
          ],
         }
      ]
    }
  }
  </code></pre>
  
  <p><strong>EDIT--</strong> Solution by using Java driver as per Sylvain Leroux</p>
  
  <pre><code>    BasicDBObject matchSurvey = new BasicDBObject();
      matchSurvey.put("$match", new BasicDBObject("json.surveyRefId", new BasicDBObject("$in", new Integer[]{377,360})));
      BasicDBObject unwind = new BasicDBObject();
      unwind.put("$unwind", "$json.questionsVoList");
      BasicDBObject matchQuestion = new BasicDBObject();
      matchQuestion.put("$match", new BasicDBObject("json.questionsVoList.questionType", "hard"));
      HashMap map = new HashMap();
      map.put("_id", "$_id");
      map.put("questionsVoList", new BasicDBObject("$push", "$json.questionsVoList"));
      BasicDBObject group = new BasicDBObject();
      group.put("$group",map);        
      List&lt;BasicDBObject&gt; list = new ArrayList&lt;BasicDBObject&gt;();
      list.add(matchSurvey);
      list.add(unwind);
      list.add(matchQuestion);
      list.add(group);
      AggregateIterable output = collection.aggregate(list, DBObject.class);
  </code></pre>