title: Spring data mongodb application design and data aggregation
body: <p>I was developing a little application with <strong><a href="https://projects.spring.io/spring-data-mongodb/" rel="nofollow noreferrer">spring data mongo</a></strong> and <strong><a href="https://angularjs.org/" rel="nofollow noreferrer">angularjs</a></strong>.
  Here is model :</p>
  
  <pre><code>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;
       }
  </code></pre>
  
  <p>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. </p>
  
  <p>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:</p>
  
  <pre><code>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();
  </code></pre>
  
  <p>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 :</p>
  
  <pre><code>{"leases" : [{"id" : 123, "created" : 12324343434, "payments" : [123, 455, 343, 323, 344, null, null, 332, 323, 232, 333, 434}]}
  </code></pre>
  
  <p>where payments will represent twelve months of a particular year. </p>
  
  <p>How can I obtain this with spring data mongodb?</p>
  
  <p>Any help would be appreciated!</p>