我正在尝试更新文档
最终更新 update = new Update();
update.set("id", String.valueOf(partnerId));
update.pushAll("appIds", appIds.toArray());
mongoTemplate.upsert(Query.query(Criteria.where("partnerId").is(partnerId)), update, PartnerAppIds.class);
执行此操作时出现此错误
{ "err": "对文档 {_id:\"66\", ...} 应用更新后,发现(不可变)字段 '_id' 已更改为 _id:\"5 }
文档看起来像这样
public class PartnerAppIds {
@Indexed
private String id;
@Indexed
private Long partnerId;
@Indexed
private Set<String> appIds;
public Long getPartnerId() {
return partnerId;
}
public void setPartnerId(Long partnerId) {
this.partnerId = partnerId;
}
public Set<String> getAppIds() {
return appIds;
}
public void setAppIds(Set<String> appIds) {
this.appIds = appIds;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
我的问题是,因为我有一个 id 字段,为什么 mongo 假设我正在更新“_id”字段,而我在这里更新 id 字段。这里 mongo 的 _id 和 id 是否相同
最佳答案
来自docs ,
A property or field without an annotation but named id will be mapped to the _id field.
When querying and updating MongoTemplate will use the converter to handle conversions of the Query and Update objects that correspond to the above rules for saving documents so field names and types used in your queries will be able to match what is in your domain classes.
因此,如果需要更新,请使用除 _id 或 id 之外的不同名称的字段。
关于java - mongodb 尝试在更新中编辑 _id 字段时抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48561802/