'Azure cosmos Db Criteria query with subdocuments - java spring boot
I'm new to azure cosmos db. i'm trying to read items from container in my spring boot application. i'm using cosmos template with criteria. lets say i have a document like this
{
"stop_id": 70021,
"stop_name": "CALTRAIN - 22ND ST STATION",
"stop_lat": 37.757692,
"stop_lon": -122.392318,
"zone_id": 3329,
"trip": [{
"trip_id": "RTD8997283",
"arrival_time": "05:40:00",
"departure_time": "05:40:00",
"stop_id": 70021,
"stop_sequence": 1
}, {
"trip_id": "RTD8997283",
"arrival_time": "05:52:00",
"departure_time": "05:52:00",
"stop_id": 70021,
"stop_sequence": 2
}]
}
if i want to fetch based on stop id, i can add criteria for stop id like this
Criteria criteria = Criteria.getInstance(CriteriaType.IS_EQUAL, "stop_id", Collections.singletonList("70021"), Part.IgnoreCaseType.ALWAYS);
CosmosQuery cosmosQuery = new CosmosQuery(criteria).with(Sort.unsorted());
Iterable<StopInfo> items = cosmosTemplate.find(cosmosQuery, StopInfo.class, "myContainer");
But if i want to add criteria for trip id, how can i do it?
Solution 1:[1]
This way you can search from array in cosmos using criteria
Map map = new HashMap();
map.put("trip_id","RTD8997283");
Criteria criteria = Criteria.getInstance(CriteriaType.ARRAY_CONTAINS, "trip", Collections.singletonList(map), Part.IgnoreCaseType.ALWAYS);
CosmosQuery cosmosQuery = new CosmosQuery(criteria).with(Sort.unsorted());
Iterable<StopInfo> items = cosmosTemplate.find(cosmosQuery, StopInfo.class, "myContainer");
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | s99889977 |