简体   繁体   English

如何在spring数据中使用elemMatch来嵌套对象从mongodb中检索数据

[英]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. 我需要一些帮助,在spring数据中使用elemMatch来查询mongodb中的数据并从mongodb中检索结果。

I want to retrieve results for a particular testRun say testRun=1 我想检索特定testRun的结果,比如testRun = 1

I have the following data in mongodb: 我在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: 在mongo控制台中,我使用了以下命令并得到了结果:

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

I got the results for my requirement in mongo console. 我在mongo控制台中得到了我的要求的结果。

The real question comes here, I used spring data in to retrieve the results in my java code. 真正的问题来了,我使用spring数据来检索我的java代码中的结果。

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. 谢谢。

You don't need $elemMatch if you would like to get "testRuns.testRun" = 1. There is no second condition. 如果你想获得“testRuns.testRun”= 1,你不需要$ elemMatch。没有第二个条件。 Please refer the below statement in the mongo reference document link. 请参阅mongo参考文档链接中的以下声明。

"Since the $elemMatch only specifies a single condition, the $elemMatch expression is not necessary, and instead you can use the following query:" “由于$ elemMatch仅指定单个条件,因此不需要$ elemMatch表达式,而是可以使用以下查询:”

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

https://docs.mongodb.com/manual/reference/operator/query/elemMatch/ 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. 请确保在Java代码中将testRun变量定义为Integer。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM