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

Exclude projection doesn't working [DATAMONGO-2582]

Open
#3,435 2 comments 0 reactions 1 assignee View on GitHub

@christophstrobl is already working on this.

Since Dec 30, 2020.

type: bug
Dominant language
Java
Stars
1.7k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

Jose Armando de Almeida Neto opened DATAMONGO-2582 and commented

Hi! Recently I had a problem with a MongoDB query that when I'm using the code below to remove unneeded fields, the response doesn't remove that.

 

Criteria offerCriteria = where("xTrackId").is(xTrackId)
        .and("offers.id").is(offerId)
        .and("lastTransaction").is(true)
        .and("dtCreate").gt(LocalDate.now().minusDays(creationDateLimitDays));

if (checkUserProfile)
    offerCriteria.and("xUserId").is(xUserId);

ProjectionOperation removeUnneededFields = project()
        .andExclude("offer.request.header",
                "offers.request.search.segment",
                "offers.request.search.businessItem.brokerageUnion",
                "offers.request.search.businessItem.insurance.brokers",
                "offers.request.search.businessItem.insurance.counterPart",
                "offers.request.search.businessItem.insurance.autos.vehicle",
                "offers.request.search.businessItem.insurance.autos.isNewVehicle",
                "offers.items.compositionTracings",
                "offers.items.package");

AggregationResults<Document> result = mongoTemplate.aggregate(newAggregation(
        match(offerCriteria),
        unwind("$offers"),
        match(where("offers.id").is(offerId)),
        removeUnneededFields
), offersCollection, Document.class);

To work around this problem, I used the MongoCollection class and I made the query as the default mongo driver:

 

MongoCollection<Document> collection = mongoTemplate.getCollection(offersCollection);

List<Bson> aggregationPipeline = new ArrayList<>(Arrays.asList(
        match(eq("xTrackId", xTrackId)),
        match(eq("lastTransaction", true)),
        match(eq("offers.id", offerId)),
        match(gt("dtCreate", LocalDate.now().minusDays(creationDateLimitDays)))
));

if (checkUserProfile)
    aggregationPipeline.add(match(eq("xUserId", xUserId)));

aggregationPipeline.addAll(Arrays.asList(
        unwind("$offers"),
        match(eq("offers.id", offerId)),
        project(exclude("offers.request.header",
                "offers.request.search.segment",
                "offers.request.search.businessItem.brokerageUnion",
                "offers.request.search.businessItem.insurance.brokers",
                "offers.request.search.businessItem.insurance.counterPart",
                "offers.request.search.businessItem.insurance.autos.vehicle",
                "offers.request.search.businessItem.insurance.autos.isNewVehicle",
                "offers.items.compositionTracings",
                "offers.items.package"
        ))
));

I hope it was useful!

 

 


No further details from DATAMONGO-2582

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.