---
title: "Get Documents Command"
url: "https://docs.ravendb.net/6.2/client-api/commands/documents/get"
version: "6.2"
language: "java"
---

# Get Documents Command

> Client language: Java. Also available for [C#](https://docs.ravendb.net/6.2/client-api/commands/documents/get.md), [Python](https://docs.ravendb.net/6.2/client-api/commands/documents/get.python.md), [PHP](https://docs.ravendb.net/6.2/client-api/commands/documents/get.php.md), [Node.js](https://docs.ravendb.net/6.2/client-api/commands/documents/get.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."

There are a few methods that allow you to retrieve documents from a database:

- [Get single document](#get-single-document)
- [Get multiple documents](#get-multiple-documents)
- [Get paged documents](#get-paged-documents)
- [Get documents by starts with](#get-by-starts-with)
- [Get metadata only](#get-metadata-only)

## Get single document

**GetDocumentsCommand** can be used to retrieve a single document

### Syntax

```java
public GetDocumentsCommand(String id, String[] includes, boolean metadataOnly)
```

| Parameter | Type | Description |
| - | - | - |
| **id** | `String` | ID of the documents to get. |
| **includes** | `String[]` | Related documents to fetch along with the document. |
| **metadataOnly** | `boolean` | Whether to fetch the whole document or just the metadata. |

### Example

```java
GetDocumentsCommand command = new GetDocumentsCommand(
  "orders/1-A", null, false);
session.advanced().getRequestExecutor().execute(command);
ObjectNode order = (ObjectNode) command.getResult().getResults().get(0);
```

## Get multiple documents

**GetDocumentsCommand** can also be used to retrieve a list of documents.

### Syntax

```java
public GetDocumentsCommand(String[] ids, String[] includes, boolean metadataOnly)
```

| Parameter | Type | Description |
| - | - | - |
| **ids** | `String[]` | IDs of the documents to get. |
| **includes** | `String` | Related documents to fetch along with the documents. |
| **metadataOnly** | `boolean` | Whether to fetch whole documents or just the metadata. |

### Example I

```java
GetDocumentsCommand command = new GetDocumentsCommand(
  new String[]{"orders/1-A", "employees/3-A"}, null, false);
session.advanced().getRequestExecutor().execute(command);
ObjectNode order = (ObjectNode) command.getResult().getResults().get(0);
ObjectNode employee = (ObjectNode) command.getResult().getResults().get(1);
```

### Example II - Using Includes

```java
// Fetch emploees/5-A and his boss.
GetDocumentsCommand command = new GetDocumentsCommand(
  "employees/5-A", new String[]{"ReportsTo"}, false);
session.advanced().getRequestExecutor().execute(command);

ObjectNode employee = (ObjectNode) command.getResult().getResults().get(0);
String bossId = employee.get("ReportsTo").asText();
ObjectNode boss = (ObjectNode) command.getResult().getIncludes().get(bossId);
```

### Example III - Missing Documents

```java
// Assuming that products/9999-A doesn't exist.
GetDocumentsCommand command = new GetDocumentsCommand(
  new String[]{"products/1-A", "products/9999-A", "products/3-A"}, null, false);
session.advanced().getRequestExecutor().execute(command);
ArrayNode products = command.getResult().getResults(); // products/1-A, null, products/3-A
```

## Get paged documents

**GetDocumentsCommand** can also be used to retrieve a paged set of documents.

### Syntax

```java
public GetDocumentsCommand(int start, int pageSize)
```

| Parameter | Type | Description |
| - | - | - |
| **start** | `int` | Number of documents that should be skipped. |
| **pageSize** | `int` | Maximum number of documents that will be retrieved. |

### Example

```java
GetDocumentsCommand command = new GetDocumentsCommand(0, 128);
session.advanced().getRequestExecutor().execute(command);
ArrayNode firstDocs = command.getResult().getResults();
```

## Get by starts with

**GetDocumentsCommand** can be used to retrieve multiple documents for a specified ID prefix.

### Syntax

```java
public GetDocumentsCommand(
  String startWith,
  String startAfter,
  String matches,
  String exclude,
  int start,
  int pageSize,
  boolean metadataOnly)
```

| Parameter | Type | Description |
| - | - | - |
| **startsWith** | `String` | Prefix for which documents should be returned. |
| **startAfter** | `String` | Skip 'document fetching' until the given ID is found, and return documents after that ID (default: null). |
| **matches** | `String` | Pipe ('\|') separated values for which document IDs (after 'startsWith') should be matched ('?' any single character, '\*' any characters). |
| **exclude** | `String` | Pipe ('\|') separated values for which document IDs (after 'startsWith') should **not** be matched ('?' any single character, '\*' any characters). |
| **start** | `int` | Number of documents that should be skipped. |
| **pageSize** | `int` | Maximum number of documents that will be retrieved. |
| **metadataOnly** | `boolean` | Specifies whether or not only document metadata should be returned. |

### Example I

```java
GetDocumentsCommand command = new GetDocumentsCommand(
  "products",  //startWith
  null, //startAfter
  null, // matches
  null, //exclude
  0, // start
  128, // pageSize
  false //metadataOnly
);

session.advanced().getRequestExecutor().execute(command);
ArrayNode products = command.getResult().getResults();
```

### Example II

```java
// return up to 128 documents  with key that starts with 'products/'
// and rest of the key begins with "1" or "2", eg. products/10, products/25
GetDocumentsCommand command = new GetDocumentsCommand(
  "products", //startWith
  null, // startAfter
  "1*|2*", // matches
  null, // exclude
  0, //start
  128, //pageSize
  false); //metadataOnly
```

### Example III

```java
// return up to 128 documents with key that starts with 'products/'
// and rest of the key have length of 3, begins and ends with "1"
// and contains any character at 2nd position e.g. products/101, products/1B1
GetDocumentsCommand command = new GetDocumentsCommand(
  "products", //startWith
  null, // startAfter
  "1?1", // matches
  null, // exclude
  0, //start
  128, //pageSize
  false); //metadataOnly
session.advanced().getRequestExecutor().execute(command);
ArrayNode products = command.getResult().getResults();
```

## Get metadata only

**GetDocumentsCommand** can be used to retrieve the metadata of documents.

### Example

```java
GetDocumentsCommand command = new GetDocumentsCommand("orders/1-A", null, true);
session.advanced().getRequestExecutor().execute(command);
      
JsonNode result = command.getResult().getResults().get(0);
ObjectNode documentMetadata = (ObjectNode) result.get("@metadata");
      
// Print out all the metadata properties.
Iterator<String> fieldIterator = documentMetadata.fieldNames();
      
while (fieldIterator.hasNext()) {
  String field = fieldIterator.next();
  JsonNode value = documentMetadata.get(field);
  System.out.println(field + " = " + value);
}
```

## See also

- [Put document](https://docs.ravendb.net/6.2/client-api/commands/documents/put.java.md)
- [Delete document](https://docs.ravendb.net/6.2/client-api/commands/documents/delete.java.md)
- [Send multiple commands using a batch](https://docs.ravendb.net/6.2/client-api/commands/batches/how-to-send-multiple-commands-using-a-batch.java.md)
