下面是我的示例 json 对象,我有不同的 ID,我想获得特定的国家 ID。用户将传递 id 和国家键作为请求,然后国家/地区我还有其他列表以及用户将作为属性传递(列表用户除外)
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 查询生成的 { "$and" : [ { "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"
}]