spring-projects / spring-data-mongodb

Provides support to increase developer productivity in Java when using MongoDB. Uses familiar Spring concepts such as a template classes for core API usage and lightweight repository style data access.
https://spring.io/projects/spring-data-mongodb/
Apache License 2.0
1.62k stars 1.09k forks source link

Compass pipeline export to Java not producing same results [DATAMONGO-2583] #3397

Closed spring-projects-issues closed 4 years ago

spring-projects-issues commented 4 years ago

samueldedavid opened DATAMONGO-2583 and commented

Mongo Pipeline

[\{$match: {[{$match: {  pd: 'PD',  type: 'type1',  date: {    $gte: '2019-01-01',    $lte: '2019-12-01'  }}}, \{$sort: {  pd: 1,  date: 1}}, \{$group: {  _id: {    site: '$site'  },  wf: {    $push: {      date: '$date',      resources: '$resources'}}
`        }  },  resources: \{    $sum: '$resources'  `}, \{$match: {  resources: {    $gt: 0  }}}]
Arrays.asList(match(and(eq("pd", "PD"), eq("type", "type1"), and(gte("date", "2019-01-01"), lte("date", "2019-12-01")))), sort(orderBy(ascending("pd"), ascending("date"))), group(eq("site", "$site"), push("wf", and(eq("date", "$date"), eq("resources", "$resources"))), sum("resources", "$resources")), match(gt("resources", 0L)));

Output from Mongo Query is in the attached file view.json

Output from Java aggregates framework is 

Document\`_id=Document{{site=T`, wf=[true, true, true, true, true, true, true, true, true, true, true, true], tresources=21.0}}}}
`2020-07-08 14:19:53.095 INFO 24544 --- [ main] com.example.services.PcfReportsService : Document\{{_id=Document{{site=G`, wf=[true, true, true, true, true, true, true, true, true, true, true, true], tresources=458.0}}}}

  The problem is that the collected items in the group are returned as True and False which is not correct and also not what is produced when using this pipeline in shell . I have no other option but to post it here as a bug . Currently I am using @aggregates to do this operation. Also It would be nice to pass the pipeline array as a single string in aggregation parameter. Currently I break the pipeline above  and add it as @aggregation(pipeline={p1,p2,p3...pn})


Reference URL: https://developer.mongodb.com/community/forums/t/compass-pipeline-export-to-java-not-producing-same-results/6362

Attachments:

spring-projects-issues commented 4 years ago

Christoph Strobl commented

I might be wrong but judging from the given snippets, it looks like you're directly using MongoDB driver API from the com.mongodb.client.model namespace to create the pipeline.

Can you share how the pipeline is executed?

spring-projects-issues commented 4 years ago

samueldedavid commented

Christoph Strobl 

I use it like this . I could not find a lot of documentation about it. 

List<Bson> pipeline = Arrays.asList( match(and(eq("pdg", "PD"), eq("type", "type1"), and(gte("date", "2019-01-01"), lte("date", "2019-12-01")))), sort(orderBy(ascending("pdg"), ascending("date"))), group(eq("site", "$site"), push("wf", and(eq("site", "$site"), eq("date", "$date"), eq("resources", "$resources"))), sum("tresources", "$resources")), match(gt("tresources", 0L)));

List<Document> result =

template.getCollection("sample").aggregate(pipeline).into(new ArrayList<Document>());

 

 

spring-projects-issues commented 4 years ago

Christoph Strobl commented

Thanks for sharing, everything past template.getCollection("sample") is MongoDB driver API. Please report issues with the driver at jira.mongodb.org

spring-projects-issues commented 4 years ago

samueldedavid commented

Christoph Strobl Ok Thanks