Package io.quarkus.mongodb.panache

API usage

MongoDB with Panache comes in two flavors, the active record pattern via PanacheMongoEntity, and the repository pattern via PanacheMongoRepository. To use the active record pattern, make your entities extend PanacheMongoEntity, use public fields for your columns, use the existing operations defined as static methods on your entity class, and define custom ones as static methods on your entity class:
 public class Person extends PanacheMongoEntity {
     public String name;
     public LocalDate birth;
     public PersonStatus status;

     public static Person findByName(String name){
       return find("name", name).firstResult();
     }

     public static List<Person> findAlive(){
       return list("status", Status.Alive);
     }

     public static void deleteStefs(){
       delete("name", "Stef");
     }
 }
 
To use the repository pattern, create a class implementing PanacheMongoRepository, use the existing operations from your repository and define new ones on your repository class:
 @ApplicationScoped
 public class PersonRepository implements PanacheRepository<Person> {
    public Person findByName(String name){
        return find("name", name).firstResult();
    }

    public List<Person> findAlive(){
        return list("status", Status.Alive);
    }

    public void deleteStefs(){
        delete("name", "Stef");
   }
 }
 
You can also use PanacheMongoRepository if you prefer the repository approach.

You can use the @MongoEntity annotation to define the name of the MongoDB collection, otherwise it will be the name of your entity.

The Mongo PojoCodec is used to serialize your entity to Bson Document, you can find more information on it's documentation page: https://mongodb.github.io/mongo-java-driver/3.10/bson/pojos/ You can use the MongoDB annotations to control the mapping to the database : @BsonId, @BsonProperty("fieldName"), @BsonIgnore.

Simplified queries

Normally, MongoDB queries are of this form: {"field1": "value1", "field2": "value2"}

We support multiple convenience query implementations, this is what we called PanacheQL queries:

  • You can use one of the three flavours or parameterized query:
    • find("field", value)
    • find("field = ?1", value)
    • find("field = :value", Parameters.with("value", value)
    They will all generates the same query : {"field": "value"}.
  • We support the following query operators: 'and', 'or' ( mixing 'and' and 'or' is not currently supported), '=', '>', '>=', '<', '<=', '!=', 'is null', 'is not null', 'in' and 'like' that is mapped to the MongoDB `$regex` operator (both String and JavaScript patterns are supported).
  • field replacement is supported based on the value of the @BsonProperty annotations

WARNING: MongoDB queries must be valid JSON documents, using the same field multiple times in a query is not allowed using PanacheQL as it would generate an invalid JSON (see this issue on github: https://github.com/quarkusio/quarkus/issues/12086).

You can also write native MongoDB queries, in this case the field names are not replaced even if you use @BsonProperty, but you can still use parameterized queries by index or name.
find("{'field':?1}", value) or find("{'field'::key}", value)

Author:
Loïc Mathieu