title: Project New Array Field with Spring Data
body: <p>As part of an aggregate operation, I need to unwind an array. I am wondering how I can put the object back into an array as part of the project. Here is the MongoDB aggregate operation that works:</p>
  
  <pre><code>db.users.aggregate([ { "$match" : {...} , { "$unwind" : "$profiles"} ,{$project: {'profiles': ['$profiles']}}...}
  </code></pre>
  
  <p>And more specifically, how can I implement this using Spring Data mongoDB ProjectionOperation:</p>
  
  <pre><code>{$project: {'profiles': ['$profiles']}}
  </code></pre>
  
  <p>This feature has been added since <a href="https://docs.mongodb.com/manual/reference/operator/aggregation/project/index.html#new-array-fields" rel="nofollow noreferrer">3.2</a>.</p>
  
  <p>Edit 1:</p>
  
  <p>I looked through some of the posts and one answer by
  <a href="https://stackoverflow.com/questions/39459898/does-spring-data-mongodb-support-filter-array-aggregations-operator/39465971#39465971">Christoph Strobl</a>:</p>
  
  <p>and based on the answer I came up with something that works which is as follows:</p>
  
  <pre><code>  AggregationOperation project = aggregationOperationContext -&gt; {
      Document projection = new Document();
      projection.put("profiles", Arrays.&lt;Object&gt; asList("$profiles"));
      projection.put("_id","$id");
      return new Document("$project", projection);
    };
  </code></pre>
  
  <p>I am wondering if there is a better way of doing it though.
  Any help/suggestion is very much appreciated. Thanks.</p>