title: MongoDB / Morphia - Projection not working on recursive objects?
body: <p>I have a test object which works as nodes on a tree, containing 0 or more children instances of the same type. I'm persisting it on MongoDB and querying it with Morphia.</p>
  
  <p>I perform the following query:</p>
  
  <pre><code>db.TestObject.find( {}, { _id: 1, childrenTestObjects: 1 } ).limit(6).sort( {_id: 1 } ).pretty();
  </code></pre>
  
  <p>Which results in:</p>
  
  <pre><code>{ "_id" : NumberLong(1) }
  { "_id" : NumberLong(2) }
  { "_id" : NumberLong(3) }
  { "_id" : NumberLong(4) }
  {
          "_id" : NumberLong(5),
          "childrenTestObjects" : [
                  {
                          "stringValue" : "6eb887126d24e8f1cd8ad5033482c781",
                          "creationDate" : ISODate("1997-05-24T00:00:00Z")
                          "childrenTestObjects" : [
                                  {
                                          "stringValue" : "2ab8f86410b4f3bdcc747699295eb5a4",
                                          "creationDate" : ISODate("2024-10-10T00:00:00Z"),
                                          "_id" : NumberLong(7)
                                  }
                          ],
                          "_id" : NumberLong(6)
                  }
          ]
  }
  </code></pre>
  
  <p>That's awesome, but also a little surprising. I'm having two issues with the results:</p>
  
  <p>1) When I do a projection, it only applies to the top elements. The children elements still return other properties not in the projection (stringValue and creationDate). I'd like the field selection to apply to all documents and sub documents of the same type. This tree has an undermined number of sub items, so I can't specify that in the query explicitly. How to accomplish that?</p>
  
  <p>2) To my surprise, limit applied to sub documents! You see that there was one embedded document with id 6. I was expecting to see 6 top level documents with N sub documents, but instead got just 5. How to tell MongoDB to return 6 top level elements, regardless of what is embedded in them? Without that having a consistent pagination system is impossible.</p>
  
  <p>All your help has made learning MongoDB way faster and I really appreciate it! Thanks!</p>