harish
harish

Reputation: 33

How to use elemMatch in spring data for nested objects to retrieve data from mongodb

I need some help on using elemMatch in spring data to query data in mongodb and retrieve results from mongodb.

I want to retrieve results for a particular testRun say testRun=1

I have the following data in mongodb:

[ {
"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"
  }
]

In mongo console i used the following command and got the results:

db.test.find({"testRuns.testRun":1},{"testRuns":{"$elemMatch":{"testRun":1}}}).pretty()

I got the results for my requirement in mongo console.

The real question comes here, I used spring data in to retrieve the results in my java 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);

However, i'm not able to retrieve the results for a particular test run. Can someone please help me resolve this issue.

Thank you.

Upvotes: 3

Views: 5365

Answers (1)

notionquest
notionquest

Reputation: 39226

You don't need $elemMatch if you would like to get "testRuns.testRun" = 1. There is no second condition. Please refer the below statement in the mongo reference document link.

"Since the $elemMatch only specifies a single condition, the $elemMatch expression is not necessary, and instead you can use the following query:"

db.survey.find(
{ "results.product": "xyz" }
)

https://docs.mongodb.com/manual/reference/operator/query/elemMatch/

I have tested with the below code with the data given in the post. It works fine.

Please make sure that the testRun variable is defined as Integer in Java code. If it is of other data type, the query wouldn't return result.

query.addCriteria(Criteria.where("testRuns.testRun").is(testRun));
mongoOperations.find(query, Stack.class);

Upvotes: 1

Related Questions