title: How to use elemMatch in spring data for nested objects to retrieve data from mongodb
body: <p>I need some help on using elemMatch in spring data to query data in mongodb and retrieve results from mongodb.</p>
  
  <p>I want to retrieve results for a particular testRun say testRun=1 </p>
  
  <p>I have the following data in mongodb:</p>
  
  <pre><code>[ {
  "testMethod": "initialization",
  "testCase": "com.harish.test.TestNGRest.OrganisationGetTest",
  "build": 1,
  "ranNumberofTimes": 2,
  "failed": 0,
  "success": 2,
  "testRuns": [
    {
      "testMethod": "initialization",
      "testCase": "com.harish.test.TestNGRest.OrganisationGetTest",
      "branchName": "branch_1",
      "testRun": 1,
      "success": 1,
      "fail": 0,
      "timetorun": 0,
      "cases": [
        {
          "caseId": 1,
          "status": "success",
          "failreason": null,
          "startDate": "Fri May 27 14:41:22 EDT 2016",
          "endDate": "Fri May 27 14:41:22 EDT 2016"
        }
      ],
      "startDate": "Fri May 27 14:41:22 EDT 2016",
      "endDate": "Fri May 27 14:41:22 EDT 2016"
    },
    {
      "testMethod": "initialization",
      "testCase": "com.harish.test.TestNGRest.OrganisationGetTest",
      "branchName": "branch_1",
      "testRun": 2,
      "success": 1,
      "fail": 0,
      "timetorun": 1,
      "cases": [
        {
          "caseId": 1,
          "status": "success",
          "failreason": null,
          "startDate": "Fri May 27 14:41:49 EDT 2016",
          "endDate": "Fri May 27 14:41:49 EDT 2016"
        }
      ],
      "startDate": "Fri May 27 14:41:49 EDT 2016",
      "endDate": "Fri May 27 14:41:49 EDT 2016"
    }
  ]
  </code></pre>
  
  <p>In mongo console i used the following command and got the results:</p>
  
  <pre><code>db.test.find({"testRuns.testRun":1},{"testRuns":{"$elemMatch":{"testRun":1}}}).pretty()
  </code></pre>
  
  <p>I got the results for my requirement in mongo console.</p>
  
  <p>The real question comes here, I used spring data in to retrieve the results in my java code.</p>
  
  <pre><code>return mongoTemplate.find(BasicQuery.query(Criteria.where("testRuns.testRun").is(testRun).andOperator(Criteria.where("testRuns").elemMatch(Criteria.where("testRun").is(testRun)))),Test.class, COLLECTION_NAME);
  </code></pre>
  
  <p>However, i'm not able to retrieve the results for a particular test run. Can someone please help me resolve this issue.</p>
  
  <p>Thank you.</p>