Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically derive the fields to be read from the expected result type [DATAMONGO-2041] #2908

spring-projects-issues opened this issue Aug 2, 2018 · 1 comment


Copy link

@spring-projects-issues spring-projects-issues commented Aug 2, 2018

Philipp Hauer opened DATAMONGO-2041 and commented

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,

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

So I have to set up the projection manually:


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:

Kind Regards,

Referenced from: pull request #591

Copy link

@spring-projects-issues spring-projects-issues commented Aug 6, 2018

Oliver Drotbohm commented

Christoph Strobl – if you fancy some API design work over or in between the performance stuff, would you mind looking into this one? In the Twitter conversation I wondered whether this is something we could add into the builder API as with the ….as(…) method we have explicit API for selecting the type to read.

Another option (or even an additional API) could be to extend the Query API to take a Class attribute which the query mapping step would the translate into the appropriate fields document

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants