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

The _id field is flatted out in the aggregation results [DATAMONGO-2237] #3101

Closed
spring-projects-issues opened this issue Mar 28, 2019 · 4 comments

Comments

@spring-projects-issues
Copy link

@spring-projects-issues spring-projects-issues commented Mar 28, 2019

Damien Arrachequesne opened DATAMONGO-2237 and commented

Hi everyone!

Reproducible test case: https://github.com/darrachequesne/spring-data-mongodb-demo

When using the following aggregation:

Price priceFromAggregation = mongoTemplate.aggregate(newAggregation(
      match(where("_id.productId").is(1))
), Price.class, Price.class).getMappedResults().get(0);

The id field in the Price object is null.

It seems the UnwrapAndReadDocumentCallback (here: https://github.com/spring-projects/spring-data-mongodb/blob/2.2.0.M2/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java#L2084) is flattening the _id field in the raw results.

// raw
{ _id: { productId: 1, storeId: 1 }, other fields... }
// mapped to
{ productId: 1, storeId: 1, other fields... }

 Is that the expected behaviour?

Thanks!

 


Affects: 2.1.5 (Lovelace SR5)

Reference URL: https://github.com/darrachequesne/spring-data-mongodb-demo

Issue Links:

  • DATAMONGO-2277 Aggregate result mapping incorrect when using composite Id
    ("is duplicated by")
  • DATAMONGO-2437 Grouping after aggregation returns NULL id when mapping to a class with compound key
    (
    "is duplicated by"_)

Referenced from: pull request #808

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Mar 29, 2019

Mark Paluch commented

This behavior is in place since we support the MongoDB Aggregation Framework. This is expected behavior.

Using typed aggregations with the same entity type as input (collection name) and output type (mapping target) breaks mapping as composite Id's are represented on top-level and this behavior comes by surprise.

We haven't documented this behavior. Let me come back to you with further details

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Apr 9, 2019

Damien Arrachequesne commented

Hi Mark, thanks a lot for the details.

A more generic example, I would have expected the following

db.getCollection("test").aggregate([
  { $group: { _id: { a: "$field_a", b: "$field_b" } } }
])

to be mapped to

@Data
public class AggregateResult {
  private AggregatePK id;

  @Data
  public static class AggregatePK {
    private int a;
    private int b;
  }
}

instead of

@Data
public class AggregateResult {
  private int a;
  private int b;
}

But if the documentation states otherwise, that's OK for us!

 

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Nov 11, 2019

Mark Paluch commented

It makes sense to revisit this issue for the next release train. Id flattening happens only in the imperative template API, not in the reactive one

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Jan 15, 2020

Mark Paluch commented

Closing as duplicate of DATAMONGO-2437

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

Successfully merging a pull request may close this issue.

None yet
2 participants