以下是 Mongo DB 中存在的數據{"name":"john","id":"123","location":"Pune"}{"name":"steve","id":"456","location":"Noida"}我想將“id”更新為“789”,將“name”更新為“alex”,其中“name”:“john”和“location”:“Pune”,并且根據 upsert 功能,如果查詢條件不存在,然后它需要創建一個新條目。我正在使用以下邏輯使用 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 portalIDjava.lang.IllegalArgumentException: Invalid BSON field name portalIDat org.bson.AbstractBsonWriter.writeName(AbstractBsonWriter.java:532)有人可以建議我嗎?
1 回答

紫衣仙女
TA貢獻1839條經驗 獲得超15個贊
試試下面的代碼:
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);
添加回答
舉報
0/150
提交
取消