You are currently browsing legacy 3.0 version of documentation. Click here to switch to the newest 4.2 version.

We can help you with migration to the latest RavenDB

Contact Us Now
see on GitHub

Session: Querying: How to query?

Database can be queried using QueryDSL-enabled query method with few custom extension that will be described later.

Syntax

public <T> IRavenQueryable<T> query(Class<T> clazz);  // perform query on a dynamic index

public <T> IRavenQueryable<T> query(Class<T> clazz, String indexName);

public <T> IRavenQueryable<T> query(Class<T> clazz, String indexName, boolean isMapReduce);

public <T> IRavenQueryable<T> query(Class<T> clazz, Class<? extends AbstractIndexCreationTask> indexCreator);

Parameters
indexName String Name of an index to perform query on
isMapReduce boolean Indicates if queried index is a map/reduce index (modifies how we treat identifier properties).
Return Value
IRavenQueryable Instance implementing IRavenQueryable interface containing additional query methods.

Safe By Default

The default value of a page size for a query is 128 results. In order to retrieve a different number of results in a single query use .Take(pageSize) method.

Example I - Basic

// load up to 128 entities from 'Employees' collection
List<Employee> employees = session
  .query(Employee.class)
  .toList();

Notice that by specifying Employee as a class parameter, we are not only defining a result type, but also marking name of collection that will be queried.

Example II - Syntax

Queries can be performed using strongly typed syntax.

// load up to 128 entities from 'Employees' collection
// where FirstName equals 'Robert'
QEmployee e = QEmployee.employee;
List<Employee> employees = session
  .query(Employee.class)
  .where(e.firstName.eq("Robert"))
  .toList();

Example III - Querying specified index

// load up to 128 entities from 'Employees' collection
// where FirstName equals 'Robert'
// using 'My/Custom/Index'
QEmployee e = QEmployee.employee;
List<Employee> employees = session
  .query(Employee.class, "My/Custom/Index")
  .where(e.firstName.eq("Robert"))
  .toList();

or

// load up to 128 entities from 'Employees' collection
// where FirstName equals 'Robert'
// using 'My/Custom/Index'
QEmployee e = QEmployee.employee;
List<Employee> employees = session
  .query(Employee.class, MyCustomIndex.class)
  .where(e.firstName.eq("Robert"))
  .toList();

Custom methods and extensions

Available custom methods and extensions: