gpt4 book ai didi

mongodb - 使用 Spring Data Mongo 的 ObjectId 问题的 Facet + 聚合查询

转载 作者:行者123 更新时间:2023-12-01 00:09:31 35 4
gpt4 key购买 nike

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

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

员工外出.java
@JsonPropertyOrder({"id","address","departments"})
public class EmployeeOut{

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

员工内部.java
public class EmployeeInner{
protected String id;
protected String firstName;
protected String lastName;
protected Integer email;
protected String status;
}

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

当查询执行 id 时总是为空,有什么建议吗?

由于安全原因,我有诸如无法粘贴查询之类的文档。
{
"_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 字段。

应该是 _id 不是身份证。还将电子邮件类型更改为字符串。为我工作。

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

关于mongodb - 使用 Spring Data Mongo 的 ObjectId 问题的 Facet + 聚合查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60057371/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com