---
title: "Project Query Results"
url: "https://docs.ravendb.net/6.2/client-api/session/querying/how-to-project-query-results"
version: "6.2"
language: "java"
---

# Project Query Results

> Client language: Java. Also available for [C#](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-project-query-results.md), [Python](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-project-query-results.python.md), [PHP](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-project-query-results.php.md), [Node.js](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-project-query-results.nodejs.md).
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

Instead of pulling full documents in query results you can just grab some pieces of data from documents. You can also transform the projected
results. The projections are defined with the usage of:

- [SelectFields](#selectfields)
- [OfType](#oftype---simple-projection)

## SelectFields

The most common way to perform a query with projection is to use the `selectFields` method. You can specify what fields from a document you want to retrieve.

### Example I - Projecting Individual Fields of the Document

#### Java

```java
// request name, city and country for all entities from 'Companies' collection
QueryData queryData = new QueryData(
  new String[] { "Name", "Address.city", "Address.country"},
  new String[] { "Name", "City", "Country"});
List<NameCityAndCountry> results = session
  .query(Company.class)
  .selectFields(NameCityAndCountry.class, queryData)
  .toList();
```

#### RQL

```sql
from Companies
select Name, Address.City as City, Address.Country as Country
```

### Example II - Projecting Arrays and Objects

#### Java

```java
QueryData queryData = new QueryData(new String[]{ "ShipTo", "Lines[].ProductName" },
  new String[]{"ShipTo", "Products"});

List<ShipToAndProducts> results = session.query(Order.class)
  .selectFields(ShipToAndProducts.class, queryData)
  .toList();
```

#### RQL

```sql
from Orders
select ShipTo, Lines[].ProductName as Products
```

### Example III - Projection with Expression

#### Java

```java
List<FullName> results = session.advanced().rawQuery(FullName.class, "from Employees as e " +
  "select {" +
  "    FullName : e.FirstName + \" \" + e.LastName " +
  "}").toList();
```

#### RQL

```sql
from Employees as e
select {
  FullName : e.FirstName + " " + e.LastName
}
```

### Example IV - Projection with `declared function`

#### Java

```java
List<Employee> results = session.advanced().rawQuery(Employee.class, "declare function output(e) { " +
  "    var format = function(p){ return p.FirstName + \" \" + p.LastName; }; " +
  "    return { FullName : format(e) }; " +
  "} " +
  "from Employees as e select output(e)").toList();
```

#### RQL

```sql
declare function output(e) {
  var format = function(p){ return p.FirstName + " " + p.LastName; };
  return { FullName : format(e) };
}
from Employees as e select output(e)
```

### Example V - Projection with Calculation

#### Java

```java
List<Total> results = session.advanced().rawQuery(Total.class, "from Orders as o " +
  "select { " +
  "    Total : o.Lines.reduce( " +
  "        (acc , l) => acc += l.PricePerUnit * l.Quantity, 0) " +
  "}").toList();
```

#### RQL

```sql
from Orders as o
select {
  Total : o.Lines.reduce(
      (acc , l) => acc += l.PricePerUnit * l.Quantity, 0)
}
```

### Example VI - Projection Using a Loaded Document

#### Java

```java
List<OrderProjection> results = session.advanced().rawQuery(OrderProjection.class, "from Orders as o " +
  "load o.Company as c " +
  "select { " +
  "    CompanyName: c.Name," +
  "    ShippedAt: o.ShippedAt" +
  "}").toList();
```

#### RQL

```sql
from Orders as o
load o.Company as c
select {
  CompanyName: c.Name,
  ShippedAt: o.ShippedAt
}
```

### Example VII - Projection with Dates

#### Java

```java
List<EmployeeProjection> results = session.advanced().rawQuery(EmployeeProjection.class, "from Employees as e " +
  "select { " +
  "    DayOfBirth : new Date(Date.parse(e.Birthday)).getDate(), " +
  "    MonthOfBirth : new Date(Date.parse(e.Birthday)).getMonth() + 1, " +
  "    Age : new Date().getFullYear() - new Date(Date.parse(e.Birthday)).getFullYear() " +
  "}").toList();
```

#### RQL

```sql
from Employees as e 
select { 
  DayOfBirth : new Date(Date.parse(e.Birthday)).getDate(), 
  MonthOfBirth : new Date(Date.parse(e.Birthday)).getMonth() + 1,
  Age : new Date().getFullYear() - new Date(Date.parse(e.Birthday)).getFullYear() 
}
```

### Example VIII - Projection with Raw JavaScript Code

#### Java

```java
List<EmployeeProjection> results = session.advanced().rawQuery(EmployeeProjection.class, "from Employees as e " +
  "select { " +
  "    Date : new Date(Date.parse(e.Birthday)), " +
  "    Name : e.FirstName.substr(0,3) " +
  "}").toList();
```

#### RQL

```sql
from Employees as e 
select {
  Date : new Date(Date.parse(e.Birthday)), 
  Name : e.FirstName.substr(0,3)
}
```

### Example IX - Projection with Metadata

#### Java

```java
List<Employee> results = session.advanced().rawQuery(Employee.class, "from Employees as e " +
  "select {" +
  "     Name : e.FirstName, " +
  "     Metadata : getMetadata(e)" +
  "}").toList();
```

#### RQL

```sql
from Employees as e 
select {
   Name : e.FirstName, 
   Metadata : getMetadata(e)
}
```

This method overload retrieves all public fields of the class given in generic and uses them to perform projection to the requested type.
You can use this method instead of using `selectFields` together with all fields of the projection class.

### Example X

#### Java

```java
List<ContactDetails> results = session.query(Company.class, Companies_ByContact.class)
  .selectFields(ContactDetails.class)
  .toList();
```

#### RQL\_1

```sql
from index 'Companies/ByContact' 
select Name, Phone
```

#### RQL\_2

```java
from index 'Companies/ByContact' 
select Name, Phone
```

#### Index

```java
private class Companies_ByContact extends AbstractIndexCreationTask {
  public Companies_ByContact() {

      map = "from c in docs.Companies select new  { Name = c.Contact.Name, Phone = c.Phone } ";

      storeAllFields(FieldStorage.YES); // name and phone fields can be retrieved directly from index
  }
}
```

## OfType - simple projection

`ofType` is a client-side projection. The easiest explanation of how it works is to take the results that the server returns and map them to given type. This may become useful when querying an index that contains fields that are not available in mapped type.

### Example

```java
// query index 'Products_BySupplierName'
// return documents from collection 'Products' that have a supplier 'Norske Meierier'
// project them to 'Products'
List<Product> results = session.query(Products_BySupplierName.Result.class, Products_BySupplierName.class)
  .whereEquals("Name", "Norske Meierier")
  .ofType(Product.class)
  .toList();
```

> **Note**
>
> Projected entities (even named types) are not tracked by the session.

> **Note**
>
> If the projected fields are stored inside the index itself (`FieldStorage.YES` in the index definition), then the query results will be created directly from there instead of retrieving documents in order to project.

## See also

- [Query Overview](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-query.md)
- [How to Stream Query Results](https://docs.ravendb.net/6.2/client-api/session/querying/how-to-stream-query-results.java.md)
- [Querying an index](https://docs.ravendb.net/6.2/indexes/querying/query-index.java.md)
- [Project index query results](https://docs.ravendb.net/6.2/indexes/querying/projections.java.md)
- [JavaScript Engine](https://docs.ravendb.net/6.2/server/kb/javascript-engine.md)
