使用Spring Data Mongo的ObjectId问题进行方面+汇总查询 [英] Facet + Aggregate Query using ObjectId issue with Spring Data Mongo

查看:741
本文介绍了使用Spring Data Mongo的ObjectId问题进行方面+汇总查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 Spring Boot + Spring Data Mongo .我真的很努力

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

EmployeeOutFacet.java

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

EmployeeOut.java

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

EmployeeInner.java

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

Employee.java

Employee.java

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

当查询执行id时始终给出null,有什么建议吗?

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",
        ....
        ...
    },
}

推荐答案

您在项目阶段和小组阶段都错误地映射了id字段.

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

它应该是 _id 而不是id.还要将电子邮件类型更改为字符串.为我工作.

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);

这篇关于使用Spring Data Mongo的ObjectId问题进行方面+汇总查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆