@Document
class Entity1 {
@Id
String id
@DbRef(lazy=true)
Entity2 entity2;
String test;
}
mongoTemplate.upsert(
new Query(Criteria.where("entity2.$id").is(entity2Id),
new Update().set("test", "newValue"),
Entity1.class);
我得到了 Found $id field without a $ref before it, which is invalid
我的问题是:如何使用 DbRef 执行此更新插入。
最佳答案
将实体传递给 where 查询,而不是 id。创建 Entity2 并将其保存到数据库,然后使用 entity2 实例并将代码更改为:where("entity2").is(entity2)
关于java - 如何使用 spring mongoTemplate 使用 DbRef 更新 MongoDB 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30665777/