gpt4 book ai didi

java - MongoDB 使用分层 JSON 进行更新 - 如何高效?点符号?

转载 作者:太空宇宙 更新时间:2023-11-04 13:31:41 25 4
gpt4 key购买 nike

Mongodb 有一个更新功能,可以增加预先存在的字段。但是,我发现它只能更新平面 JSON。每当 JSONObject 内部有一个 JSONObject,并且我想要增加一个值时,我实际上似乎无法做到这一点。它将返回此错误:

com.mongodb.WriteConcernException: Write failed with error code 14 and error message 
'Cannot increment with non-numeric argument: {laneQty: { BOTTOM: 1 }}'

如您所见,我尝试将 LaneQty.BOTTOM 递增 1 进行更新。我不想编写算法将每个分层 json 字段更改为点表示法(如 LaneQty.BOTTOM),那么有没有办法将 JSON 转换为点表示法预插入?

现在我的常规 upsert 函数如下所示:

public boolean incrementJson(BasicDBObject json, String colName, ArrayList<String> queryParams, ArrayList<String> removeParams){
/*make sure the game id AND the main player id can't both be the same.
If either/or, it's fine. We don't want duplicates.
*/
BasicDBObject query = new BasicDBObject();
DBCollection collection = db.getCollection(colName);
for(int i = 0; i < queryParams.size(); i++){
String param = queryParams.get(i);
query.put(param, json.get(param));
}
for(String param : removeParams){
json.remove(param);
}
return collection.update(query, new BasicDBObject("$inc", json), true, false).isUpdateOfExisting();
}

是否有对此代码的任何建议升级,可以使其轻松更新分层 json?谢谢!

顺便说一下,我很难对此进行硬编码。有大量的分层对象,这将花费我很长时间。另外,我无法完全控制图层中填充哪些字段,因此我不能每次都只说laneQty.BOTTOM,因为它不会始终存在。在更新插入之前,BasicDBObject json 实际上是解析为 BasicDBObject 的 java bean。如果有帮助的话,这是它的构造函数:

public ChampionBean(int rank, int division, int assists, int deaths, int kills, int qty, int championId,
HashMap<String, Integer> laneQty, HashMap<String, Integer> roleQty,
ParticipantTimelineDataBean assistedLaneDeathsPerMinDeltas,
ParticipantTimelineDataBean assistedLaneKillsPerMinDeltas, ParticipantTimelineDataBean creepsPerMinDeltas,
ParticipantTimelineDataBean csDiffPerMinDeltas, ParticipantTimelineDataBean damageTakenDiffPerMinDeltas,
ParticipantTimelineDataBean damageTakenPerMinDeltas, ParticipantTimelineDataBean goldPerMinDeltas,
ParticipantTimelineDataBean xpDiffPerMinDeltas, ParticipantTimelineDataBean xpPerMinDeltas, int wins,
int weekDate, int yearDate) {
super();
this.rank = rank;
this.division = division;
this.assists = assists;
this.deaths = deaths;
this.kills = kills;
this.qty = qty;
this.championId = championId;
this.laneQty = laneQty;
this.roleQty = roleQty;
this.assistedLaneDeathsPerMinDeltas = assistedLaneDeathsPerMinDeltas;
this.assistedLaneKillsPerMinDeltas = assistedLaneKillsPerMinDeltas;
this.creepsPerMinDeltas = creepsPerMinDeltas;
this.csDiffPerMinDeltas = csDiffPerMinDeltas;
this.damageTakenDiffPerMinDeltas = damageTakenDiffPerMinDeltas;
this.damageTakenPerMinDeltas = damageTakenPerMinDeltas;
this.goldPerMinDeltas = goldPerMinDeltas;
this.xpDiffPerMinDeltas = xpDiffPerMinDeltas;
this.xpPerMinDeltas = xpPerMinDeltas;
this.wins = wins;
this.weekDate = weekDate;
this.yearDate = yearDate;
}

participantTimelineDataBean 是另一个 Bean,里面有 4 个 int 字段。我想增加这些字段(所以是的,它只有 2 层深,所以如果有一个具有 2 层深度可用性的解决方案,我也会采用)。

最佳答案

使用点符号:

 new BasicDBObject("$inc", new BasicDBObject("laneQty.BOTTOM", 1) )

另一种快速而肮脏的解决方案:只需collection.save同一_id下的整个文档。

关于java - MongoDB 使用分层 JSON 进行更新 - 如何高效?点符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32133937/

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