gpt4 book ai didi

java - 当 Bson 过滤器通过时,Mongo 函数 updatemany() 不起作用

转载 作者:行者123 更新时间:2023-12-02 01:09:41 25 4
gpt4 key购买 nike

以下是 Mongo DB 中存在的数据

{"name":"john","id":"123","location":"Pune"}
{"name":"steve","id":"456","location":"Noida"}

我想将“id”更新为“789”,将“name”更新为“alex”,其中“name”:“john”和“location”:“Pune”并根据更新插入功能,如果查询条件是不存在,则需要创建一个新条目。

我使用以下逻辑使用 Bson 过滤器来执行此操作,但出现以下异常

Bson filter=null;
Bson update=null;

filter=combine(eq("name":"john"),eq("location":"Pune"));
update=combine(eq("id":"123"),eq("name":"alex"));
UpdateOptions options = new UpdateOptions();
options.upsert(true);
dbCollection.updateMany(filter, update,options);

我期望我的 Mongo DB 数据发生以下变化:

{"name":"alex","id":"789","location":"Pune"}

但是我遇到了异常:

Exception is java.lang.IllegalArgumentException: Invalid BSON field name portalID
java.lang.IllegalArgumentException: Invalid BSON field name portalID
at org.bson.AbstractBsonWriter.writeName(AbstractBsonWriter.java:532)

有人可以推荐我吗?

最佳答案

尝试以下代码:

Bson filter = null;
Bson update = null;

filter = and(eq("name", "john"), eq("location", "Pune"));
update = combine(set("id", "789"), set("name", "alex"));
UpdateOptions options = new UpdateOptions();
options.upsert(true);
dbCollection.updateMany(filter, update, options);

关于java - 当 Bson 过滤器通过时,Mongo 函数 updatemany() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57697322/

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