Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complex mongodb query with Quarkus

I need to migrate a Spring Boot project to Quarkus. The project has been using Spring Data Mongodb for all the queries. Now I find it difficult to migrate complex queries.

One example is

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

How can I implement the conditional query with Quarkus?

like image 997
user3923099 Avatar asked May 01 '20 16:05

user3923099


People also ask

Does MongoDB allow ad hoc queries?

Ad-hoc queries are the queries not known while structuring the database. So, MongoDB provides ad-hoc query support which makes it so special in this case. Ad-hoc queries are updated in real time, leading to an improvement in performance.

Can MongoDB handle millions of records?

Working with MongoDB and ElasticSearch is an accurate decision to process millions of records in real-time. These structures and concepts could be applied to larger datasets and will work extremely well too.


1 Answers

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

like image 136
Dmytro Chaban Avatar answered Oct 02 '22 21:10

Dmytro Chaban