gpt4 book ai didi

java - 通过 Spring Boot Rest API 查询 mongodb 集合以分页格式返回结果

转载 作者:行者123 更新时间:2023-12-02 09:13:03 25 4
gpt4 key购买 nike

Mongo 中存在一个具有一些属性的集合。每当引擎中发生任何事务时,都会保存此信息。我需要一个 API,使用户能够通过以下属性的一个或多个组合来搜索 AnaysisJob:• 开始者•startAt(我们需要“from”和“to”,并且我们使用 Between 子句进行搜索)。• 状态。

用户可以使用单个属性或上述属性的组合进行搜索。如果使用组合,则必须进行“AND”运算。

不要忘记,它也必须支持分页。

以下是类(class):

AnalysisJobSearchCriteria.java

@Data
@Document
public class AnalysisJobSearchCriteria {
private Long startedAt = System.currentTimeMillis();
private String startedBy;
private AnalysisJobStatusType status;
}

AnalysisJobResource.java

@Slf4j
@Api(value = "Analysis Job Resource", tags = "CONNECTIONS")
@RestController
@RequestMapping("/api/v1")
public class AnalysisJobResource {

@Autowired
private AnalysisJobSearchService analysisJobSearchService;

@ApiOperation(value = "Analysis", response = Map.class)
@ApiResponses({
@ApiResponse(code = 400, message = "Bad Request"),
@ApiResponse(code = 404, message = "Not found"),
@ApiResponse(code = 200, message = "OK")
})
@PostMapping(value = "/analysis-job", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> searchAnalysisJob(@RequestBody final AnalysisJobSearchCriteria analysisJobSearchCriteria) {
AggregationResults<AnalysisJobResource> analysisJob = analysisJobSearchService.searchAnalysisJobs(analysisJobSearchCriteria);
return new ResponseEntity<>(analysisJob, HttpStatus.OK);
}
}

AnalysisJobSearchService.java

@Service
public class AnalysisJobSearchService {

private final AnalysisJobCustomRepositoryImpl analysisJobCustomRepositoryImpl;

@Autowired
public AnalysisJobSearchService(final AnalysisJobCustomRepositoryImpl analysisJobCustomRepositoryImpl) {
this.analysisJobCustomRepositoryImpl = analysisJobCustomRepositoryImpl;
}

public AggregationResults<AnalysisJobResource> searchAnalysisJobs(AnalysisJobSearchCriteria analysisJobSearchCriteria) {
AggregationResults<AnalysisJobResource> analysisJob = analysisJobCustomRepositoryImpl.searchAnalysisJob(analysisJobSearchCriteria);
return analysisJob;
}

}

AnalysisJobCustomRepository.java

@SuppressWarnings("unchecked")
@Override
public AggregationResults<AnalysisJobResource> searchAnalysisJob(AnalysisJobSearchCriteria analysisJobSearchCriteria) {
MatchOperation salaryMatch = null;
if (!analysisJobSearchCriteria.getStatus().equals(null)) {
salaryMatch = Aggregation.match(new Criteria("status").is(analysisJobSearchCriteria.getStatus()));
}

Aggregation aggregation = Aggregation.newAggregation(salaryMatch);

AggregationResults<AnalysisJobResource> result = mongoTemplate.aggregate(
aggregation, "result", AnalysisJobResource.class);
return result;
}

这些是我的一些类(class)。我还有一个 Connection 类来与我的数据库建立连接,并且连接工作正常。只是我无法提出一个查询以及 mongo 模板来让我的场景工作。

由于我刚开始使用 Mongo 和 Spring Boot,因此我们将不胜感激。另外,我相信如果服务返回 List 而不是 AggregationResult 会更好。但是如何在 Java 中为 Mongo 创建查询让我很困惑。

最佳答案

您可以使用 AggregationOperation 来解决此问题

private List<SearcheableEntity> searchSmth(CustomCriteria criteria){

List<AggregationOperation> operations = new ArrayList<>();

//it's simple way, better pattern is to create criteria builder,
//witch will create Criteria.class object from request params
operations.add(Aggregation.match(Criteria.where("name").is(criteria.getName());

//For pagination skip and limit methods can be used
operations.add(Aggregation.skip(criteria.getOffset() != null ? criteria.getOffset() : 0));
operations.add(Aggregation.limit(criteria.getLimit() != null ? criteria.getLimit() : Integer.MAX_VALUE));

return mongoTemplate
.aggregate(Aggregation.newAggregation(operations),
"Collection_name",
SearcheableEntity.class
).getMappedResults();
}

此外,如果您需要在搜索中使用逻辑“或”或“与”,您可以以此类模式创建搜索条件

new Criteria().andOperator(firstCrinteria, ..., lastCriteria)
new Criteria().orOperator(firstCrinteria, ..., lastCriteria)

举个例子

SuppressWarnings("unchecked")
@Override
public List<AnalysisJobResource> searchAnalysisJob(AnalysisJobSearchCriteria criteria) {
List<AggregationOperation> operations = new ArrayList<>();

if (!criteria.getStatus() != null) {
operations.add(Aggregation.match(Criteria.where("status").is(criteria.getStatus())));
}
if(!criteria.getStartedAt().equals(null)){
operations.add(Aggregation.match(Criteria.where("time").gt(criteria.getStartedAt()));
}

return mongoTemplate.aggregate(
Aggregation.newAggregation(operations), "result", AnalysisJobResource.class).getMappedResults();

}

关于java - 通过 Spring Boot Rest API 查询 mongodb 集合以分页格式返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59239596/

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