title: How to filter embedded array in mongo document with morphia
body: <p>Given my Profile data looks like below, I want to find the profile for combination of userName and productId
   and only return the profile with the respective contract for this product.</p>
  
  <pre><code>{
      "firstName": "John",
      "lastName": "Doe",
      "userName": "john.doe@gmail.com",
      "language": "NL",
      "timeZone": "Europe/Amsterdam",
      "contracts": [
          {
              "contractId": "DEMO1-CONTRACT",
              "productId": "ticket-api",
              "startDate": ISODate('2016-06-29T09:06:42.391Z'),
              "roles": [
                  {
                      "name": "Manager",
                      "permissions": [
                          {
                              "activity": "ticket",
                              "permission": "createTicket"
                          },
                          {
                              "activity": "ticket",
                              "permission": "updateTicket"
                          },
                          {
                              "activity": "ticket",
                              "permission": "closeTicket"
                          }
                      ]
                  }
              ]
          },
          {
              "contractId": "DEMO2-CONTRACT",
              "productId": "comment-api",
              "startDate": ISODate('2016-06-29T10:27:45.899Z'),
              "roles": [
                  {
                      "name": "Manager",
                      "permissions": [
                          {
                              "activity": "comment",
                              "permission": "createComment"
                          },
                          {
                              "activity": "comment",
                              "permission": "updateComment"
                          },
                          {
                              "activity": "comment",
                              "permission": "deleteComment"
                          }
                      ]
                  }
              ]
          }
      ]
  }    
  </code></pre>
  
  <p>I managed to find the solution how to do this from the command line. But I don't seem to find a way how to accomplish this with Morphia (latest version).</p>
  
  <pre><code>db.Profile.aggregate([
      { $match: {"userName": "john.doe@gmail.com"}},
      { $project: {
          contracts: {$filter: {
              input: '$contracts',
              as: 'contract',
              cond: {$eq: ['$$contract.productId', "ticket-api"]}
          }}
      }}
  ])
  </code></pre>
  
  <p>This is what I have so far.  Any help is most appreciated</p>
  
  <pre><code>Query&lt;Profile&gt; matchQuery = getDatastore().createQuery(Profile.class).field(Profile._userName).equal(userName);
  getDatastore()
       .createAggregation(Profile.class)
       .match(matchQuery)
       .project(Projection.expression(??))
  </code></pre>
  
  <p>Note... meanwhile I found another solution which does not use an aggregation pipeline.</p>
  
  <pre><code>    public Optional&lt;Profile&gt; findByUserNameAndContractQuery(String userName, String productId) {
          DBObject contractQuery = BasicDBObjectBuilder.start(Contract._productId, productId).get();
          Query&lt;Profile&gt; query =
                  getDatastore()
                          .createQuery(Profile.class)
                          .field(Profile._userName).equal(userName)
                          .filter(Profile._contracts + " elem", contractQuery)
                          .retrievedFields(true, Profile._contracts + ".$");
          return Optional.ofNullable(query.get());
      } 
  </code></pre>