简体   繁体   中英

Quarkus Panache Mongodb possible queries

While using Quarkus with Panache I get pretty frustrated as I wish to understand how its working but can't as its not debug friendly. Some things are written too abstract here and I wish to just write panache queries without deep knowledge of the panache, BSON parser, MongoDB driver, mongodb,... implementations.

  1. Can I use a Collection on status in?1 ?
  2. How to debug queries? Sometimes I get empty result on a like query eg status in?1 but don't know why
  3. Can someone please provide examples for the usual operators like AND , OR , IN , EQUALS , GREATER THAN ,...?
  4. How to index fields out of the code.

There are many other examples on stack overflow which I can't even follow anymore as I don't get how to write and know these usages. It just doesn't look simple anymore. I am trying since month to get my application to work on production but failing always on the database level. Please help to get more understanding of panache.

With MongoDB with Panache you can write queries in two styles:

  1. PanacheQL, which is a subset of JPQL (the query language of JPA), a query language very close to the WHERE clause of an SQL query.
  2. Native MongoDB query, for convenience JSON can be written with single quote instead of double quote.

The easiest is of course PanacheQL, while using native MongoDB query you can embrace the full power of MongoDB query language (but you'll need to write JSON query).

If you are using PanacheQL, you can check the generated query by setting DEBUG log level to the logger io.quarkus.mongodb.panache.runtime as explained in the Query debugging of the guide.

On the guide, there is some examples of PanacheQL queries, if you feel that more should be added please open an enhanement request with some example you would want to be added (or even a PR.).

To answer your questions regarding the queries

Can I use a Collection on status in?1?

Yes, this is supported.

How to debug queries?

You can enable query logging, and play the same query directly on MongoDB (using the mongo shell or any MongoDB UI).

Can someone please provide examples for the usual operators like AND, OR, IN, EQUALS, GREATER THAN,...?

There is examples on the Simplified queries section and we provide the list of operators supported, among us AND, OR (but you can't mix both), IN, =, >=, <=, ... There isn't an example for all of them but you can find plenty example of SQL WHERE clause with them.

Here are a few sample (not tested)

People.list("name = ?1 and country = ?2", "Doe", "France");
People.list("name = ?1 or country = ?2", "Doe", "France");
People.list("age >= ?1 or cage <= ?2", 10, 60);
People.list("country in ?1", List.of("France", "Germany"));

How to index fields out of the code

If you talk about automatic MongoDB index creation, it's not supported. There is however enhancement request to support it, or to more generally support MongoDB database migration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM