The first experiments with Spring Data and MongoDB were great. Now I have the following structure (simplified):
public class Letter { @Id private String id; private List<Section> sections; } public class Section { private String id; private String content; }
Downloading and saving entire objects / documents Letter works like a charm. (I use ObjectId to generate unique identifiers for the Section.id field.)
Letter letter1 = mongoTemplate.findById(id, Letter.class) mongoTemplate.insert(letter2); mongoTemplate.save(letter3);
Since the documents are large (200 KB), and sometimes applications only need parts: is it possible to request a subdocument (section), change and save it? I would like to implement a method like
Section s = findLetterSection(letterId, sectionId); s.setText("blubb"); replaceLetterSection(letterId, sectionId, s);
And, of course, methods such as:
addLetterSection(letterId, s); // add after last section insertLetterSection(letterId, sectionId, s); // insert before given section deleteLetterSection(letterId, sectionId); // delete given section
I see that the last three methods are somewhat "strange", i.e. downloading the entire document, changing the collection and saving it again may be the best approach from an object-oriented point of view; but the first use case (βtransitionβ to a sub-object / sub-object and working in the area of ββthis object) seems natural.
I think MongoDB can update subdocuments, but can SpringData be used to map objects? Thanks for any pointers.