Mongo 函数 updatemany() 在传递 Bson 过滤器时不起作用



下面是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数据库数据会发生以下变化:

{"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);   

最新更新