Uploaded image for project: 'Spring Data MongoDB'
  1. Spring Data MongoDB
  2. DATAMONGO-2041

Automatically derive the fields to be read from the expected result type

    XMLWordPrintable

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Minor
    • Resolution: Complete
    • Affects Version/s: None
    • Fix Version/s: 2.1 RC2 (Lovelace)
    • Component/s: None
    • Labels:
      None

      Description

      Dear Spring Data Team,

      by default, the MongoTemplate doesn't look at the provided class to determine the required fields. So the projection is not restricted and the whole document is retrieved.

      For example, the following code is only interested in the id field of the document:

      // class:
      @Document(collection = "entities")
      data class EntityId(
      val id: ObjectId
      )
      
      // usage:
      val query = Query()
      val entities = mongoTemplate.find(query, EntityId::class.java)
      

      But when I look at the Mongo logs, I see an empty projection. 

      So I have to set up the projection manually:

      query.fields().include("id")
      

      Would it be a useful feature if the MongoTemplate automatically scans the class and create a projection clause containing only the relevant fields?

      I already had a short discussion with Oliver about this topic on Twitter: https://twitter.com/philipp_hauer/status/1023945360589434882

      Kind Regards,
      Philipp

        Attachments

          Activity

            People

            Assignee:
            cstrobl Christoph Strobl
            Reporter:
            philipp_hauer Philipp Hauer
            Last updater:
            Oliver Drotbohm
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: