spring-projects / spring-projects/spring-data-mongodb

Fields and projection API are very inconvenient [DATAMONGO-2637]

Open
#3,490 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

in: core status: ideal-for-contribution type: bug
Dominant language
Java
Stars
1.7k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

Zsombor opened DATAMONGO-2637 and commented

We use a couple of projections in our app to limit the amount of data to transfer, with something like this:

aggregationSteps += Aggregation.project(
   "aaaa",
   "bbbb",
   "cccc",
   "dddd"
 ....  (20 more similar lines)

However, in a refactor, we moved a couple of fields into a sub-entity, and blindly adjusted the projection to this:

aggregationSteps += Aggregation.project(
     "aaaa",
     "bbbb",
     "xxx.cccc",
     "dddd"
  ... (20 more similar lines)

Which surprisingly doesn't work as expected.

The smallest solution, which I could find is:

val fields = listOf(
     "aaaa",
     "bbbb",
     "xxx.cccc",
     "dddd"
  ... (20 more similar lines)
).map { Fields.field(it, it) }

aggregationSteps += Aggregation.project(Fields.from(*fields.toTypedArray()))

So the issue is:

  • Aggregation.project(
    String... names)and Fields.fields(
    String... names) has a surprising default behavior
  • There is no API which takes an array of strings, or a list of strings as field names for projections
  • There is no API which takes a list of Fields for projections

 

 


Affects: 3.0.4 (Neumann SR4)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the Aggregation.project(String... names) and Fields.fields(String... names) APIs and their projection-related types. Check how nested names such as "xxx.cccc" are interpreted, then determine the API changes needed for string collections and lists of Fields. Done means projections support the requested field forms without the current surprising behavior, with regression coverage for nested fields.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, mongodb
Domain
api, database
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.