I have a following JSON object in database,
{
"_id": "FlightLegPaxRecord::UA::141::2019-09-07::ORD::DEN",
"activeInd": "0",
"additionalProperties": {},
"arrivalAirport": "DEN",
"carrierCode": "UA",
"departureAirport": "ORD",
"departureDate": "2019-09-07",
"docType": "FlightLegPaxRecord",
"flightNumber": "141",
"fltLclOrigDt": "2019-09-07",
"travelerCounts": [{
"additionalProperties": {},
"counts": [{
"additionalProperties": {},
"key": "J",
"value": "7"
}, {
"additionalProperties": {},
"key": "Y",
"value": "0"
}],
"travelCountType": "ActionStatus"
}, {
"additionalProperties": {},
"counts": [{
"additionalProperties": {},
"key": "J",
"value": "44"
}, {
"additionalProperties": {},
"key": "Y",
"value": "274"
}],
"travelCountType": "ActualCapacity"
}]
}
Following is my java code to update the value field of the count array which matches a travelCountType.
Following is my java code:
Map<String, String> queryMap = new HashMap<>();
queryMap.put("_id", "FlightLegPaxRecord::UA::141::2019-09-07::ORD::DEN");
queryMap.put("travelerCounts.travelCountType", "ActionStatus");
queryMap.put("travelerCounts.counts.key", "J");
Map<String, String> updQuery = new HashMap<>();
updQuery.put("travelerCounts.counts.$.value", "99");
Document query = constructDocument(queryMap);
Document setData = constructDocument(updQuery);
Document update = new Document();
update.append("$set", setData);
MongoCollection collection = database.getCollection(colName);
UpdateResult result = collection.updateOne(query, update);
private Document constructDocument(Map<String, String> searchQuery) {
Iterator<Map.Entry<String, String>> searchIterator = searchQuery.entrySet().iterator();
Document query = new Document();
while (searchIterator.hasNext()) {
Map.Entry<String, String> entry = searchIterator.next();
query.append(entry.getKey(), entry.getValue());
}
return query;
}
After executing the code, i am getting the following exception
Caused by: com.mongodb.MongoWriteException: Cannot create field 'counts' in element {travelerCounts: [ { additionalProperties: {}, counts: [ { additionalProperties: {}, key: "J", value: "7" }, { additionalProperties: {}, key: "Y", value: "0" } ], travelCountType: "ActionStatus" }, { additionalProperties: {}, counts: [ { additionalProperties: {}, key: "J", value: "44" }, { additionalProperties: {}, key: "Y", value: "274" } ], travelCountType: "ActualCapacity" } ]}
at com.mongodb.client.internal.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:967)
at com.mongodb.client.internal.MongoCollectionImpl.executeUpdate(MongoCollectionImpl.java:951)
at com.mongodb.client.internal.MongoCollectionImpl.updateOne(MongoCollectionImpl.java:613)
at com.mongodb.client.internal.MongoCollectionImpl.updateOne(MongoCollectionImpl.java:608)
at com.ual.noc.connector.service.MongoCrudServiceImpl.updateSingleDocument(MongoCrudServiceImpl.java:90)
at com.ual.noc.connector.TestMongo.testUpdate(TestMongo.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:363)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:307)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:136)
... 17 more
I am not able to understand where i am going wrong. Please help me in resolving the issue. Thanks in advance. Please let me know if i need to elaborate the issue in more detail.
travelerCountsarray without any positional operator