Ich bin mit Feder-Boot-Version 1.4.2 und 3.4.2 mongodbMongoDB nicht Aktualisierung von Dokumenten durchführen, nachdem nur ein Teil eines Dokuments Feld Aktualisierung
Wenn ich die Repository-Schnittstelle verwenden, nachdem ein bereits vorhandenes Dokument anhalten Wenn Sie eine der Eigenschaften des Dokuments ändern, wird das alte Dokument gespeichert. Zum Beispiel:
@Document(collection="UserDoc)
public class UserDoc{
@Id
private String id;
private String userName;
private String passWord;
private int age;
//getters and setters...
}
@Autowired
private UserDocRepository userDocRepository;
UserDoc userDoc = userDocRepository.findOne("dummyId");
//assume the id passed in is correct and a UserDoc is returned.
//now when i change one of the fields,eg the age and save it back,the old
//value gets persisted,eg if age is 10 and i change it to 5,the old property
//is still persisted to db unless i Create a new UserDoc and copy all the
//properties before it gets persisted with the correct value of 5.
userDoc.setAge(5);
userDocRepository.save(userDoc);//this is supposed to update the document
//with a new value of 5 instead of 10 as it was probably per the assumption.
UserDoc userDocUpdated = new UserDoc();
userDocUpdated.setId(userDoc.getId);
userDocUpdated.setUserName(userDoc.getUserName);
userDocUpdated.setPassWord(userDoc.getPassWord);
userDocUpdated.setAge(5); //this way,the document is updated with 5 as the
//age
userDocRepository.save(userDocUpdated);//this updates it accordingly with
//the desired value of 5 for the age
Was ist das Problem mit dem ersten Szenario könnte Könnte es ein Problem mit mongodb oder Federverschluß sein Keine Erkenntnisse gerne geschätzt werden?.
überprüfen Sie das zweite Mal, wenn Sie "userRepository" aufrufen, sollte userRepository.save (userD sein oc); –
@ PaoloMangia, ich verstehe dich nicht, kannst du genauer sein? – Rbk