-->

Facet + Aggregate Query using ObjectId issue with

2020-07-17 04:37发布

问题:

I am developing Spring Boot + Spring Data Mongo. I am really struggling to

public Page<EmployeeOut> getData(Pageable pageable) {
    .......
    .......

    MatchOperation matchStage = Aggregation.match(criteria);

    GroupOperation groupOp = Aggregation
            .group("firstName", "lastName", "email", "status", "id")
            .addToSet("department").as("department").addToSet("address").as("address");

    ProjectionOperation projectStage = Aggregation.project("firstName", "lastName", "email", "status", "department", "address", "id");

    SkipOperation skipOp = Aggregation.skip((long) pageRequest.getPageNumber() * pageRequest.getPageSize());

    LimitOperation limitOp = Aggregation.limit(pageRequest.getPageSize());

    SortOperation sortOperation = ReferenceUtil.getSortOperation(pageRequest);

    FacetOperation facetOp1 = Aggregation.facet(unwind, matchStage, projectStage, groupOp).as("facet1")
            .and(unwind, matchStage, projectStage, groupOp, sortOperation, skipOp, limitOp).as("facet2");

    Aggregation aggregation = Aggregation.newAggregation(facetOp1);

    AggregationResults<EmployeeOutFacet> EmployeeOutList = mongoTemplate.aggregate(aggregation, Employee.class, EmployeeOutFacet.class);;

    .....
    return page;
}

EmployeeOutFacet.java

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class EmployeeOutFacet {
    protected List<EmployeeOut> facet1;
    protected List<EmployeeOut> facet2; 
}

EmployeeOut.java

@JsonPropertyOrder({"id","address","departments"})
public class EmployeeOut{

    @JsonProperty(value="id")
    protected EmployeeInner _id;
    protected List<Department> departments;
    protected List<Address> address;
}

EmployeeInner.java

public class EmployeeInner{
    protected String id;
    protected String firstName;
    protected String lastName;
    protected Integer email;
    protected String status;
}

Employee.java

@Document
public class Employee {
    @Id
    private String id;
    @Field
    private String firstName;
    ....
    ....
    ...
}

When the Query executes id gives always null, any suggestions?

I've documents like can't paste query due to security.

{
    "_id" : ObjectId("5cb825e566135255e0bf38a4"),
    "firstName" : "John",
    "lastName" : "Doe",
    "email" : "john.doe@gmail.com",
    .........
    .........
    "Addresss" : [ 
        {
            "Address1" : "Address 1",
            .....
            .....
            .....
        }, 
        {
            "Address1" : "Address 11",
            .....
            .....
            .....
        },
        {
            "Address1" : "Address 12",
            .....
            .....
            .....
        },
    ],
    "department" : {
        "departmentCd" : "E",
        ....
        ...
    },
}

回答1:

You are mapping the id field incorrectly in the both project and group stage.

It should be _id not id. Also change the email type to String. Works for me.

GroupOperation groupOp = Aggregation
            .group("firstName", "lastName", "email", "status", "_id")
            .addToSet("department").as("department").addToSet("address").as("address");

ProjectionOperation projectStage = Aggregation.project("firstName", "lastName", "email", "status", "department", "address", "_id");

...

AggregationResults<EmployeeOutFacet> EmployeeOutList = mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(Employee.class), EmployeeOutFacet.class);