首页 > 解决方案 > 使用 Quarkus 进行复杂的 mongodb 查询

问题描述

我需要将 Spring Boot 项目迁移到 Quarkus。该项目一直使用 Spring Data Mongodb 进行所有查询。现在我发现很难迁移复杂的查询。

一个例子是

public List<MyData> findInProgressData(MyConditions conditions) {
    Query mongoQuery = new Query();

    mongoQuery.addCriteria(Criteria.where("status").is(IN_PROGRESS));
    mongoQuery.addCriteria(Criteria.where("location").is(conditions.getLocationCode()));

    if (StringUtils.isNotBlank(conditions.getType())) {
        mongoQuery.addCriteria(Criteria.where("type").is(conditions.getType()));
    }

    if (StringUtils.isNotBlank(conditions.getUserId())) {
        mongoQuery.addCriteria(Criteria.where("userId").is(conditions.getUserId()));
    }

    mongoQuery.with(Sort.by(Sort.Direction.ASC, "createdAt"));

    return mongoTemplate.find(mongoQuery, MyData.class);
}

如何使用 Quarkus 实现条件查询?

标签: mongodbspring-dataquarkusquarkus-panache

解决方案


You can probably try Panache with Mongodb. You can pass Document into .find() method, In this object you can specify any criteria.

        final Document document = new Document();
        document.put("status","IN_PROGRESS");
        ...
        result = MyData.find(document); // or MyDataRepository

But you'll need to adapt some of the code to Panache, which can be done either via extending PanacheEntity or PanacheEntityBase


推荐阅读