I have a document having the following structure:
report {
_id : "Jan-2018-0" // (Month-Year-Version)
month : "Jan",
year : 2018,
version : 0,
data : [......]
}
The list of data is really huge. Now my use case is to create a new report with version 1 which consists of the existing data in version 0.
EDIT : Its not just updating version. Its creating a duplicate of the existing document rather.
The easiest way is to read from db version 0 and update the id as "Jan-2018-1" and then save.
But since the list of data is huge, I think it is not good to read the entire document.
So for now, I'm reading the document by avoiding the data field and then save the report after updating the id. And then read the data in pages and and append it to new version.
Something like this:
To save the new version without data
Report report = reportRepository.findReport("Jan", 2018, 0); // ignores data field
report.setId("Jan-2018-1");
report.setVersion(1);
reportRepository.save(report);
Pseudo code to populate data:
List<Data> datas = reportRepository.getDataByPages("Jan", 2018, 0, 0//offset, 100 //limit);
reportRepository.addData("Jan", 2018, 1, datas);
Is there any other better approaches than this one? Using java spring mongo.