Commands: Documents: Get

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

Get single document

GetDocumentsCommand can be used to retrieve a single document

Syntax

public GetDocumentsCommand(String id, String[] includes, boolean metadataOnly)
Parameters
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

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

public GetDocumentsCommand(String[] ids, String[] includes, boolean metadataOnly)
Parameters
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

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

// 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

// 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

public GetDocumentsCommand(int start, int pageSize)
Parameters
start int number of documents that should be skipped
pageSize int maximum number of documents that will be retrieved

Example

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

public GetDocumentsCommand(
    String startWith,
    String startAfter,
    String matches,
    String exclude,
    int start,
    int pageSize,
    boolean metadataOnly)
Parameters
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

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

// 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

// 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();