使用Quarkus的复杂mongodb查询

时间:2020-05-01 16:35:56

标签: mongodb spring-data quarkus quarkus-panache

我需要将一个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实现条件查询?

1 个答案:

答案 0 :(得分:1)

您可能可以尝试Panache with Mongodb。 您可以将Document传递到.find()方法中。在此对象中,您可以指定任何条件。

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

但是您需要使某些代码适应Panache,可以通过扩展PanacheEntityPanacheEntityBase

来完成。