I was developing a little application with spring data mongo and angularjs. Here is model :
public class Lease {
@Id
private String id;
private long created;
private Lessor lessor;
private Lessee lessee;
}
public class Payment {
@Id
private String id;
private Integer month;
private Integer year;
private Long amount;
private String leaseId;
}
I did not embed Payment model as I need it to have an id and edit it in its own form. On the main page of the app it represents a list of leases with payments by month. The year is chosen in a select above the list.
How to better load leases with payments by year by month using rest: may be first load leases then inject ids to payments or there is a better solution? For the moment I chose to use aggregation:
LookupOperation lookupOperation = LookupOperation.newLookup().
from("payment").
localField("leaseId").
foreignField("id").
as("payments");
AggregationOperation match = Aggregation.match(Criteria.where("payments.year").is(year));
Aggregation aggregation = Aggregation.newAggregation(lookupOperation, match);
return mongoOperations.aggregate(aggregation, "lease", LeaseAggregation.class).getMappedResults();
The problem is that match is exclusive here and for example if there are no payments in 2017, list of leases is empty. I saw "addFields" functionality in mongo by it is not yet implemented in spring data mongodb. A better json return for me would be :
{"leases" : [{"id" : 123, "created" : 12324343434, "payments" : [123, 455, 343, 323, 344, null, null, 332, 323, 232, 333, 434}]}
where payments will represent twelve months of a particular year.
How can I obtain this with spring data mongodb?
Any help would be appreciated!