以下是 Mongo DB 中存在的數(shù)據(jù){"name":"john","id":"123","location":"Pune"}{"name":"steve","id":"456","location":"Noida"}我想將“id”更新為“789”,將“name”更新為“alex”,其中“name”:“john”和“l(fā)ocation”:“Pune”,并且根據(jù) upsert 功能,如果查詢條件不存在,然后它需要?jiǎng)?chuàng)建一個(gè)新條目。我正在使用以下邏輯使用 Bson 過濾器執(zhí)行此操作,但出現(xiàn)以下異常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 數(shù)據(jù)發(fā)生以下變化:{"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貢獻(xiàn)1839條經(jīng)驗(yàn) 獲得超15個(gè)贊
試試下面的代碼:
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);
添加回答
舉報(bào)
0/150
提交
取消