【问题标题】:Mongo function updatemany() is not working when Bson filters are passed通过 Bson 过滤器时,Mongo 函数 updatemany() 不起作用
【发布时间】:2019-08-28 17:23:00
【问题描述】:

以下是 Mongo DB 中的数据

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

如果查询条件为不存在,则需要创建一个新条目。

我正在使用以下逻辑使用 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)

有人可以推荐我吗?

【问题讨论】:

    标签: java mongodb bson


    【解决方案1】:

    试试下面的代码:

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

    【讨论】:

    • @PavanKumar 如果您对答案满意,请采纳。
    猜你喜欢
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    • 2016-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多