title: Springdata mongodb aggregation match
body: <p>After asking question to understand a bit more of the aggregation framework in MongoDB I finally found the way to do aggregation for my need (thanks to a StackExchange user)</p>
  
  <p>So basically here is a document from my collection:</p>
  
  <pre><code>    {
          "_id" : ObjectId("s4dcsd5s4d6c54s6d"),
          "items" : [
              {
                  type : "TYPE_1",
                  text : "blablabla"
              },
              {
                  type : "TYPE_2",
                  text : "blablabla"
              },
              {
                  type : "TYPE_3",
                  text : "blablabla"
              },
              {
                  type : "TYPE_1",
                  text : "blablabla"
              },
              {
                  type : "TYPE_2",
                  text : "blablabla"
              },
              {
                  type : "TYPE_1",
                  text : "blablabla"
              }
          ]
          }
  </code></pre>
  
  <p>The idea was to be able to filter only some elements of my collections (avoiding Type 2 and 3). In fact I have more than 30 types and 6 are not allowed but for simplicity I made this example.
  So the aggregation command in command line is this one:</p>
  
  <pre><code>    db.history.aggregate([{
               $match: {
                   _id: ObjectId("s4dcsd5s4d6c54s6d")
               }
           }, {
               $unwind: '$items'
           }, {
               $match: {
                   'items.type': { '$nin': [ "TYPE_2" , "TYPE_3"] }
               }
           },
           { $limit: 10 }
       ]);
  </code></pre>
  
  <p>With this I am able to retrieve the 10 elements items of this document which do not match <code>TYPE_2</code> and <code>TYPE_3</code></p>
  
  <p>However when I am using spring data there is no output. I looked a bit at the example to build mine but its still not working.</p>
  
  <p>So I did:</p>
  
  <pre><code>Aggregation aggregation = newAggregation(
      match(Criteria.where("id").is(myID)), 
      unwind("items"),
      match(Criteria.where("items.type").nin(ignoreditemstype)), 
      limit(3), 
      skip(offsetLong)
  );
  
  AggregationResults&lt;PersonnalHistory&gt; results = mongAccess.getOperation().aggregate(query,
              "items", PersonnalHistory.class);
  </code></pre>
  
  <p><code>PersonnalHistory</code> is marked with annotation <code>@Document(collection = "history")</code> and id with the <code>@id</code> annotation</p>
  
  <p><code>ignoreditemstype</code> is a list containing <code>TYPE_2</code> and <code>TYPE_3</code></p>
  
  <p>Here is what I have in the <code>toString</code> method of aggregation:</p>
  
  <pre><code>{ 
      "aggregate" : "__collection__" , 
      "pipeline" : [ 
          { "$match": { "id" : "s4dcsd5s4d6c54s6d"} }, 
          { "$unwind": "$items"},
          { "$match": { "items.type": { "$nin" : [ "TYPE_2" , "TYPE_3" ] } } }, 
          { "$limit" : 3}, 
          { "$skip" : 0 }
      ]
  }
  </code></pre>
  
  <p>I tried a lot of stuff (to have at least an answer :) ) like removing id or the nin:</p>
  
  <pre><code>aggregation = newAggregation(
      unwind("items"),
      match(Criteria.where("items.type").nin(ignoreditemstype)), 
      limit(3), 
      skip(offsetLong)
  );
  
  aggregation = newAggregation(
      match(Criteria.where("id").is(myid)), 
      unwind("items")
  );
  </code></pre>
  
  <p>For information when I do a simple query like:</p>
  
  <pre><code>query.addCriteria(Criteria.where("id").is(myID));
  </code></pre>
  
  <p>My document is returned. However I have thousands of items. So I just want to have the 15 first (in fact the 15 first are the 15 last added)</p>
  
  <p>Do you maybe see what I am doing wrong?</p>