Annotation Interface Query


Annotation to declare finder queries directly on repository methods. Both attributes allow using a placeholder notation of ?0, ?1 and so on.
Author:
Oliver Gierke, Thomas Darimont, Christoph Strobl, Mark Paluch, Jorge Rodríguez
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    Defines the collation to apply when executing the query.
    boolean
    Returns whether the query defined should be executed as count projection.
    boolean
    Returns whether the query should delete matching documents.
    boolean
    Returns whether the query defined should be executed as exists projection.
    Defines the fields that should be returned for the given query.
    The name of the index to use.
    The mode of the read preference to use.
    Defines a default sort order for the given query.
    Takes a MongoDB JSON string to define the actual query to be executed.
  • Element Details

    • value

      String value
      Takes a MongoDB JSON string to define the actual query to be executed. This one will take precedence over the method name then.
      Returns:
      empty String by default.
      Default:
      ""
    • fields

      String fields
      Defines the fields that should be returned for the given query. Note that only these fields will make it into the domain object returned.
      Returns:
      empty String by default.
      Default:
      ""
    • count

      boolean count
      Returns whether the query defined should be executed as count projection.
      Returns:
      false by default.
      Since:
      1.3
      Default:
      false
    • exists

      boolean exists
      Returns whether the query defined should be executed as exists projection.
      Returns:
      false by default.
      Since:
      1.10
      Default:
      false
    • delete

      boolean delete
      Returns whether the query should delete matching documents.
      Returns:
      false by default.
      Since:
      1.5
      Default:
      false
    • sort

      String sort
      Defines a default sort order for the given query. NOTE: The so set defaults can be altered / overwritten using an explicit Sort argument of the query method.
       
      
                      @Query(sort = "{ age : -1 }") // order by age descending
                      List<Person> findByFirstname(String firstname);
       
       
      Returns:
      empty String by default.
      Since:
      2.1
      Default:
      ""
    • collation

      Defines the collation to apply when executing the query.
       // Fixed value
       @Query(collation = "en_US")
       List<Entry> findAllByFixedCollation();
      
       // Fixed value as Document
       @Query(collation = "{ 'locale' :  'en_US' }")
       List<Entry> findAllByFixedJsonCollation();
      
       // Dynamic value as String
       @Query(collation = "?0")
       List<Entry> findAllByDynamicCollation(String collation);
      
       // Dynamic value as Document
       @Query(collation = "{ 'locale' :  ?0 }")
       List<Entry> findAllByDynamicJsonCollation(String collation);
      
       // SpEL expression
       @Query(collation = "?#{[0]}")
       List<Entry> findAllByDynamicSpElCollation(String collation);
       
      Returns:
      an empty String by default.
      Since:
      2.2
      Default:
      ""
    • hint

      The name of the index to use. @Query(value = "...", hint = "lastname-idx") can be used as shortcut for:
       @Query(...)
       @Hint("lastname-idx")
       List<User> findAllByLastname(String collation);
       
      Returns:
      the index name.
      Since:
      4.1
      See Also:
      Default:
      ""
    • readPreference

      The mode of the read preference to use. This attribute (@Query(value = "...", readPreference = "secondary")) is an alias for:
       @Query(...)
       @ReadPreference("secondary")
       List<User> findAllByLastname(String lastname);
       
      Returns:
      the index name.
      Since:
      4.2
      See Also:
      Default:
      ""