以下は、国の特定のIDを取得したいさまざまなIDを持つ私のサンプルjsonオブジェクトです。ユーザーはリクエストとしてidとcountriesキーを渡します。それ以外の国には他のリストがあり、ユーザーは属性として渡します(リストユーザーを除く)
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"
}]
}
{ "$and" : [ { "Id" : "691021"} , { "countries" : { "$regex" : "countries"}}]} のように生成された mongodb クエリのように試してみました
私は以下のような出力を除いています
"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"
}]