gpt4 book ai didi

java - MongoDB 无法更新 java 中的嵌套数组

转载 作者:行者123 更新时间:2023-12-02 09:08:19 24 4
gpt4 key购买 nike

我在数据库中有一个以下 JSON 对象,

{
"_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"
}]
}

以下是我的 java 代码,用于更新与 travelCountType 匹配的 count 数组的 value 字段。

以下是我的java代码:

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;
}

执行代码后,出现以下异常

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

我不明白我哪里出了问题。请帮助我解决问题。提前致谢。如果我需要更详细地阐述该问题,请告诉我。

最佳答案

解决方案是使用 update filtered positional operator .

Java 代码:

Bson queryFilter = eq( "_id", "FlightLegPaxRecord::UA::141::2019-09-07::ORD::DEN" );

List<Bson> arrayFilters = new ArrayList<>();
arrayFilters.add(new Document( "tc.travelCountType", "ActionStatus" )); // "tc" for "travelerCounts"
arrayFilters.add(new Document( "ct.key", "J" )); // "ct" for "travelerCounts.counts"
UpdateOptions​ updateOptions = new UpdateOptions​().arrayFilters(arrayFilters);

Bson update = set( "travelerCounts.$[tc].counts.$[ct].value", "99" );

UpdateResult result = collection.updateOne(queryFilter, update, updateOptions);


<小时/>[ 编辑添加]

代码 updQuery.put("travelerCounts.counts.$.value", "99") 将产生错误不正确的结果。根据 MongoDB 文档 $ and Nested Arrays

The positional $ operator cannot be used for queries which traverse more than one array....

查询中的更新是一个两层嵌套数组。因此,解决方案可以是以下之一:

  1. 我已经提供了(上面)。
  2. 您还可以使用聚合 update方法。 MongoDB 版本 4.2 具有允许使用的功能聚合管道而不是更新。
  3. 如果您使用的是 MongoDB 4.0 或更早版本,则可以使用聚合,然后更新聚合结果。

关于java - MongoDB 无法更新 java 中的嵌套数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59620130/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com