mongodb如何使用Criteria Object获取列表

时间:2015-11-17 05:32:48

标签: java mongodb spring-data-mongodb mongorepository

下面是我的示例json对象我有不同的ID我希望获得特定的国家ID。用户将传递id和countries Key作为请求,然后我有其他列表的国家以及用户将作为属性传递(列出用户除外)

Criteria criteria = new Criteria().andOperator(
Criteria.where("Id").is(Id).and(attribute).is(attribute));
Query searchUserQuery = new Query(criteria);

// to print the mongodb query for debug purposes
System.out.println(criteria.getCriteriaObject());
Query searchUserQuery = new Query(criteria);
Info result =mongoTemplate .findOne(searchUserQuery, Info.class,COLLECTION);

来自数据库的Json格式

{
  "Id": "691021",
  "Type": "PR",
  "countries": [{
    "country": "AUS",
    "isoCode": "124",
    "startDate": "2015-08-17T12:30:44.789-4:00",
    "endDate": "2015-08-17T12:30:44.789-4:00"
  }, {
    "country": "US",
    "isoCode": "456",
    "startDate": "2015-11-11T12:30:44.789-4:00",
    "endDate": "2017-08-17T12:30:44.789-4:00"
  }]
}

我尝试像我的mongodb查询一样生成{{$和“:[{”Id“:”691021“},{”countries“:{”$ regex“:”countries“}}}}

我的输出除外如下

"countries": [{
  "country": "AUS",
  "isoCode": "124",
  "startDate": "2015-08-17T12:30:44.789-4:00",
  "endDate": "2015-08-17T12:30:44.789-4:00"
}, {
  "country": "US",
  "isoCode": "456",
  "startDate": "2015-11-11T12:30:44.789-4:00",
  "endDate": "2017-08-17T12:30:44.789-4:00"
}]

0 个答案:

没有答案