How to update MongoDB document with DbRef using spring mongoTemplate

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

      

I got Found $id field without a $ref before it, which is invalid

My question is how to do this with DbRef.

+3


source to share


1 answer


Pass the object to where the request is, not the id. Create and save Entity2 in DB, then use entity2 instance and change your code to: where ("entity2"). Is (entity2)



0


source







All Articles