sort subdocument in mogodb4.0 by java driver - java

I am trying to sort subdocument by Java. I am not able to find the desired output. My data set is :
[
{
"_id": {
"$oid": "5b91668a0f77e30c11574c88"
},
"driverId": "22",
"busId": "55",
"startTime": {
"$date": 1536255626852
},
"location": [
{
"latitude": 18.5803721,
"longitude": 73.7447051,
"position": 0,
"status": 1,
"time": {
"$date": 1536255628848
}
},
{
"latitude": 18.5803721,
"longitude": 73.7447051,
"position": 1,
"status": 2,
"time": {
"$date": 1536255656122
}
},
{
"latitude": 18.5803721,
"longitude": 73.7447051,
"position": 1,
"status": 2,
"time": {
"$date": 1536255656167
}
}
]
},
{
"_id": {
"$oid": "5b8c2cc70f77e322617c1ba1"
},
"driverId": "22",
"busId": "55",
"startTime": {
"$date": 1535913159533
},
"location": [
{
"latitude": 18.5804663,
"longitude": 73.7447209,
"position": 0,
"status": 1,
"time": {
"$date": 1535913160226
}
},
{
"latitude": 18.5804663,
"longitude": 73.7447209,
"position": 1,
"status": 2,
"time": {
"$date": 1535913186460
}
},
{
"latitude": 18.5804663,
"longitude": 73.7447209,
"position": 1,
"status": 2,
"time": {
"$date": 1535913187603
}
}
]
}
]
and the code i wrote :
AggregateIterable<Document> findIterable = tripsCollection.aggregate(Arrays.asList(
new Document("$match", new Document("busId", busId)),
new Document("$sort", new Document("startTime", -1)),
new Document("$sort", new Document("location.position", -1))));
When I am sorting the document by _Id or startTime, output comes accordingly. But when I am trying to sort subdocument, result set does not changes.
I tried some other variation as well:
Bson bsonFilterBus = Filters.eq("busId", busId);
Bson sortByDate = descending("startTime");
Bson sortByPosition = descending("location.position");
FindIterable<Document> findIterable = tripsCollection.find(bsonFilterBus).sort(sortByDate).sort(sortByPosition);
But results were same. i.e not sorted based on location.position
I am working with MongoDb 4.0. Somewhere I read, Mongo DB does not provide any method to sort subdocuments. Please help me out.

You have to $unwind the sub documents followed by $sort by location's position field and $group to get back location array.
Something like
AggregateIterable<Document> findIterable = tripsCollection.aggregate(
Arrays.asList(
new Document("$match", new Document("busId", busId)),
new Document("$unwind", "$location"),
new Document("$sort", new Document("location.position", -1)),
new Document("$group",
new Document("_id", "$_id")
.append("driverId", new Document("$first","$driverId"))
.append("busId", new Document("$first","$busId"))
.append("startTime", new Document("$first","$startTime"))
.append("location", new Document("$push","$location"))
),
new Document("$sort", new Document("startTime", -1))
)
);

Related

JSON Object Navigation to nested value

I am new to JSON and trying to manipulate JSON for some validation
My JSON looks like this . I need to pick the JSON object based on the refcode and then get count of different object in that, and navigate deeper inside to get the key value pair. Can someone guide me how I can navigate.
{
"components": [
{
"id": 12,
"text": "ABC",
"refCode": "CO_ABC",
"patternCode": "0",
"components": [
{
"id": 1234,
"text": "types",
"refCode": "CO_TYPES",
"questions": [
{
"questionId": 122324,
"questionText": "Is this you",
"questionSequence": 1,
"questionRefCode": "QN_STAY",
"hasPreselectedAnswer": false,
"responsesMetadata": {
"cardinality": "single",
"patternCode": "5",
"dataType": "STRING",
"numberMin": null,
"numberMax": null
},
"choices": [
{
"choiceId": 5456,
"choiceRefCode": "YES",
"choiceText": "Yes",
"sequence": 1
},
{
"choiceId": 8798,
"choiceRefCode": "NO",
"choiceText": "No",
"sequence": 2
}
],
"editable": true,
"accessible": true
}
]
},
{
"id": 13,
"text": "State of stay",
"refCode": "CO_STATE",
"questions": [
{
"questionId": 1,
"questionText": "Which state do you stay",
"questionSequence": 2,
"questionRefCode": "QN_STATE",
"hasPreselectedAnswer": false,
"responsesMetadata": {
"cardinality": "multiple",
"patternCode": "1",
"dataType": "STRING",
"numberMin": null,
"numberMax": null
},
"choices": [
{
"choiceId": 1,
"choiceRefCode": "CH_AZ",
"choiceText": "Arizona",
"sequence": 1
},
{
"choiceId": 2,
"choiceRefCode": "CH_PA",
"choiceText": "Pennsylvania",
"sequence": 2
}
],
"accessible": true
}
]
}
]
}
]
}

How to programmatically create a filter query in Elasticsearch with filtring in a nested object

I am struggling creating a filter query for Elastisearch in Java Spring. I have this Elasticsearch mapping:
"mappings": {
"properties": {
"id": {"type": "keyword"},
"name": {"type": "text"},
"city": {"type": "keyword"},
"cars": {
"type": "nested",
"properties": {
"id": {"type": "long"},
"brand": {"type": "text"},
"category": {"type": "text"}
}
}
}
Data in Elasticsearch looks like this:
[
{
"id": 1,
"name": "Car seller 1",
"city": "Detroit",
"cars": [
{"id": 1, "brand": "bmw", "category": "suv"},
{"id": 2, "brand": "bmw", "category": "sedan"},
{"id": 3, "brand": "bmw", "category": "hatchback"}
]
},
{
"id": 2,
"name": "Car seller 2",
"city": "Detroit",
"cars": [
{"id": 1, "brand": "vw", "category": "suv"},
{"id": 2, "brand": "vw", "category": "sedan"},
{"id": 3, "brand": "vw", "category": "suv"}
]
},
{
"id": 3,
"name": "Car seller 3",
"city": "Las Vegas",
"cars": [
{"id": 1, "brand": "ford", "category": "suv"},
{"id": 2, "brand": "ford", "category": "sedan"},
{"id": 3, "brand": "ford", "category": "hatchback"}
]
}
]
Now, let's say that from my FE will came an request to search documents by some filters:
1. city: ["Detroit"]
2. cars.category: ["suv"]
What I want to achieve is a filter query that will return documents of sellers, but only with cars that match the filter (if there is such a filter).
For example, for mentioned filters query should return this:
[
{
"id": 1,
"name": "Car seller 1",
"city": "Detroit",
"cars": [
{"id": 1, "brand": "bmw", "category": "suv"}
]
},
{
"id": 2,
"name": "Car seller 2",
"city": "Detroit",
"cars": [
{"id": 1, "brand": "vw", "category": "suv"},
{"id": 3, "brand": "vw", "category": "suv"}
]
}
]
So far, I was able to create query with java builders provided by Elastic, which can filter documents properly, but it returning all sellerĀ“s cars.
public Flux<Seller> filterSearch(Map<String, List<String>> filters) {
List<String> listOfFields = new ArrayList<>(filters.keySet());
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
listOfFields.forEach(field -> {
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
if (field.contains("cars.")) {
filters.get(field).forEach(value -> boolQueryBuilder.should(QueryBuilders.termQuery(field, value)));
boolQueryBuilder.minimumShouldMatch(1);
NestedQueryBuilder nestedQueryBuilder = new NestedQueryBuilder("cars", boolQueryBuilder, NestedQueryBuilder.parseScoreMode("none"));
nestedQueryBuilder.innerHit(new InnerHitBuilder());
queryBuilder.must().add(nestedQueryBuilder);
} else {
filters.get(field).forEach(value -> boolQueryBuilder.should(QueryBuilders.matchPhraseQuery(field, value)));
queryBuilder.must().add(boolQueryBuilder);
}
});
return executeSearchQuery(queryBuilder);
}
Thanks for your help.
I think you should try this:
if (field.contains("cars.")) {
...
} else {
filters.get(field).forEach(value -> boolQueryBuilder.should(QueryBuilders.matchPhraseQuery(field, value)).minimumShouldMatch(1));
queryBuilder.must().add(boolQueryBuilder);
}

How to calculate the sum of a Respond (Json) using response.path?

Seem to be struggling to calculate the number of squad members who have the role type of 'PLAYER' by performing my restassured logic against my Json response.
Currently I have a simple setup which hits an endpoint and then executes my query, I can see that the endpoint and response is valid however it seems there is a problem when attempting to filter via my query in order to calculate the total sum of squad members who have a role of 'PLAYER'.
My Rest-assured code:
#Test
public void locatePlayerCalculateSum() {
Response response = given()
.spec(footballCompetitions_requestSpecification)
.when().get(EndPoint.TEAMS + EndPoint.SQUAD);
int sum = response.path("squad.collect { it.role == \"PLAYER\" }.sum()");
System.out.println(sum);
}
Exception Message: java.lang.IllegalArgumentException: No signature of method: java.lang.Boolean.plus() is applicable for argument types: (java.lang.Boolean) values: [true]
Possible solutions: is(java.lang.Object), or(java.lang.Boolean), implies(java.lang.Boolean), and(java.lang.Boolean), use([Ljava.lang.Object;), split(groovy.lang.Closure)
Example JSON response:
"id": 66,
"area": {
"id": 2072,
"name": "England"
},
"activeCompetitions": [
{
"id": 2021,
"area": {
"id": 2072,
"name": "England"
},
"name": "Premier League",
"code": "PL",
"plan": "TIER_ONE",
"lastUpdated": "2019-01-03T23:39:45Z"
},
{
"id": 2001,
"area": {
"id": 2077,
"name": "Europe"
},
"name": "UEFA Champions League",
"code": "CL",
"plan": "TIER_ONE",
"lastUpdated": "2018-12-13T18:55:02Z"
}
],
"name": "Manchester United FC",
"shortName": "Man United",
"tla": "MNU",
"crestUrl": "http://upload.wikimedia.org/wikipedia/de/d/da/Manchester_United_FC.svg",
"address": "Sir Matt Busby Way Manchester M16 0RA",
"phone": "+44 (0161) 8688000",
"website": "http://www.manutd.com",
"email": "enquiries#manutd.co.uk",
"founded": 1878,
"clubColors": "Red / White",
"venue": "Old Trafford",
"squad": [
{
"id": 3188,
"name": "David De Gea",
"position": "Goalkeeper",
"dateOfBirth": "1990-11-07T00:00:00Z",
"countryOfBirth": "Spain",
"nationality": "Spain",
"shirtNumber": 1,
"role": "PLAYER"
},
{
"id": 3202,
"name": "Sergio Romero",
"position": "Goalkeeper",
"dateOfBirth": "1987-02-22T00:00:00Z",
"countryOfBirth": "Argentina",
"nationality": "Argentina",
"shirtNumber": null,
"role": "PLAYER"
},
{
"id": 7942,
"name": "Lee Grant",
"position": "Goalkeeper",
"dateOfBirth": "1983-01-27T00:00:00Z",
"countryOfBirth": "England",
"nationality": "England",
"shirtNumber": 13,
"role": "PLAYER"
},
{
"id": 3206,
"name": "Marcos Rojo",
"position": "Defender",
"dateOfBirth": "1990-03-20T00:00:00Z",
"countryOfBirth": "Argentina",
"nationality": "Argentina",
"shirtNumber": 16,
"role": "PLAYER"
},```
This should work:
#Test
public void locatePlayerCalculateSum() {
Response response = given()
.spec(footballCompetitions_requestSpecification)
.when().get(EndPoint.TEAMS + EndPoint.SQUAD);
int sum = response.path("squad.count { it.role == 'PLAYER' }");
System.out.println(sum);
}

How to write a REST API method for which consume json format for list of objects

I have written a method as following.
#POST
#Path("/add")
#Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
#Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
public boolean createMeasurement(List<MeasurementBean> list ,#HeaderParam(AUTHORIZATION) String authString){
Gson gson = new Gson();
LoginSession loginSession=null;
try{
loginSession = LoginUtility.validateKey(authString);
ResultRO<List<HashMap<String, Object>>> resultRO = loginSession
.execute(new Callable<ResultRO<List<HashMap<String, Object>>>>() {
#Override
public ResultRO<List<HashMap<String, Object>>> call() throws Exception {
String key[] = idKey.split(":");
String type = key[0];
long testId = Long.parseLong("2");
return doGetCreateMeasurement(list ,2);
}
});
return gson.toJson(true);
}catch(Exception ex){
ex.printStackTrace();
}
return null;
}
I am not able to consume the list of objects from json format.
and my json is in following format
{
"beans": [{
"id": 1133,
"testConditionGroupId": 0,
"testId": 0,
"type": 0,
"completeFlag": 0,
"invalidFlag": 0,
"retentionFlag": 0,
"delBeforeFlag": 0,
"dontReplicateFlag": 0,
"releaseFlag": 0,
"timeBase": 0.0,
"name": "MeaResult_TEST_XYZABCD",
"version": "V.1.0.1",
"description": "M_Test",
"mimeType": "MIME",
"stardDate": "",
"endDate": "",
"attributeList": [{
"id": 0,
"name": "Test_Attribute1",
"value": "A",
"unit": "ms"
}, {
"id": 0,
"name": "Test_Attribute2",
"value": "B",
"unit": "ms"
}],
"subMatrixList": [{
"id": 0,
"name": "Test_SubMatrixBean1_New",
"version": "V.1.0.0",
"mimeType": "MimeTyep",
"NumberOfvalues": 3,
"noOfRows": 1,
"localColumnList": [{
"id": 0,
"name": "TestName_1_New",
"version": "V.1.0.0",
"mimeType": "MimeTyep",
"sequence_Representation": "explicit",
"independent": 1,
"global_Flag": 15,
"raw_Datatype": 1,
"dataValues": ["1", "2", "3"],
"flags": [15, 15, 15],
"generation_Parameters": [1.0, 2.0, 3.0],
"externalComponents": [],
"attributeList": [],
"meaQuantity": {
"id": 0,
"name": "TestName_1_New",
"unit": "ms",
"quantityName": "MeaQuantity_Quantity",
"description": "MeaQuantity_Test",
"localName": "TestName_1_New",
"size": 10,
"dataType": 1,
"min": 1.0,
"max": 10.0,
"attributeList": [{
"id": 0,
"name": "Test_Attribute1",
"value": "A",
"unit": "ms"
}, {
"id": 0,
"name": "Test_Attribute2",
"value": "B",
"unit": "ms"
}]
}
}],
"attributeList": [{
"id": 0,
"name": "Test_Attribute1",
"value": "A",
"unit": "ms"
}, {
"id": 0,
"name": "Test_Attribute2",
"value": "B",
"unit": "ms"
}]
}],
"meaQuantityList": [{
"id": 0,
"name": "TestName_1_New",
"unit": "ms",
"quantityName": "MeaQuantity_Quantity",
"description": "MeaQuantity_Test",
"localName": "TestName_1_New",
"size": 10,
"dataType": 1,
"min": 1.0,
"max": 10.0,
"attributeList": [{
"id": 0,
"name": "Test_Attribute1",
"value": "A",
"unit": "ms"
}, {
"id": 0,
"name": "Test_Attribute2",
"value": "B",
"unit": "ms"
}]
}]
}]
}
but when I am trying to POST request using postman it gives following error
WARNING: No message body reader has been found for request class MeasurementBeans, ContentType : application/json.
and also method is not executing.
You are accepting a List<MeasurementBean> in the createMeasurement() method and the JSON you have mentioned is an object.
{
"beans": []
//..
}
Just make it as an array by removing the starting {, "beans:" and ending }, i.e you just need to pass the array.

Get value from a object

my collection contains documents like this:
{
"_id": ObjectId("585a7886e4b06aec5d1d1639"),
"name": "somename",
"owner": "someowner",
"slots": 50,
"gold": 0,
"tag": "sometext",
"motd": "sometext",
"purchases": [],
"members": {
"membername1": {
"rank": 2
},
"membername2": {
"rank": 5
},
"membername3": {
"rank": 3
}
}
}
I need to get int value from "members.membername.rank".
how do I get this value, knowing only membername?
The following code excludes the document's ID and gets only the rank for memername1
List<Document> pipeline = Arrays.asList(new Document("$project", new Document("rank", "$members.membername1.rank").append("_id", false)));
return ApiResponse.withBody(coll.aggregate(pipeline).first().getInteger("rank"));

Categories

Resources