Commands: Documents: Get

There are few methods that allow you to retrieve documents from a database:
- Get
- Get - multiple documents
- GetDocuments
- StartsWith

Get

Get can be used to retrieve a single document.

Syntax

public JsonDocument get(String key);
Parameters
key String key of the document you want to retrieve
Return Value
JsonDocument Object representing the retrieved document.

Example

JsonDocument document = store.getDatabaseCommands().get("products/1"); // null if does not exist

Get - multiple documents

Get can also be used to retrieve a list of documents.

Syntax

public MultiLoadResult get(String[] ids, String[] includes);

public MultiLoadResult get(String[] ids, String[] includes, String transformer);

public MultiLoadResult get(String[] ids, String[] includes, String transformer, Map<String, RavenJToken> transformerParameters);

public MultiLoadResult get(String[] ids, String[] includes, String transformer, Map<String, RavenJToken> transformerParameters, boolean metadataOnly);
Parameters
keys String[] array of keys of the documents you want to retrieve
includes String[] array of paths in documents in which server should look for a 'referenced' document (check example)
transformer String name of a transformer that should be used to transform the results (default: null)
transformerParameters Map<string, RavenJToken> inputs (parameters) that will be used by transformer (default: null)
metadataOnly boolean specifies if only document metadata should be returned (default: false)

public class MultiLoadResult
{
  private List<RavenJObject> results;
  private List<RavenJObject> includes;

  public List<RavenJObject> getResults() {
    return results;
  }

  public void setResults(List<RavenJObject> results) {
    this.results = results;
  }

  public List<RavenJObject> getIncludes() {
    return includes;
  }

  public void setIncludes(List<RavenJObject> includes) {
    this.includes = includes;
  }
}
Return Value
results List<RavenJObject> list of documents in exact same order as in keys parameter
includes List<RavenJObject> list of documents that were found in specified paths that were passed in includes parameter

Example I

MultiLoadResult resultsWithoutIncludes = store.getDatabaseCommands().get(new String[] { "products/1", "products/2" }, null);

Example II - using includes

MultiLoadResult resultsWithIncludes = store.getDatabaseCommands().get(
  new String[] { "products/1", "products/2" },
  new String[] { "Category" });

List<RavenJObject> results = resultsWithIncludes.getResults(); // products/1, products/2
List<RavenJObject> includes = resultsWithIncludes.getIncludes(); // categories/1

Example III - missing documents

// assuming that 'products/9999' does not exist
MultiLoadResult resultsWithIncludes = store.getDatabaseCommands().get(
  new String[] { "products/1", "products/9999", "products/3" },
  null
  );
List<RavenJObject> results = resultsWithIncludes.getResults(); // products/1, null, products/3
List<RavenJObject> includes = resultsWithIncludes.getIncludes(); // empty

GetDocuments

GetDocuments can be used to retrieve multiple documents.

Syntax

public List<JsonDocument> getDocuments(int start, int pageSize);

public List<JsonDocument> getDocuments(int start, int pageSize, boolean metadataOnly);
Parameters
start int number of documents that should be skipped
pageSize int maximum number of documents that will be retrieved
metadataOnly boolean specifies if only document metadata should be returned (default: false)
Return Value
JsonDocument Object representing retrieved document.

Example

List<JsonDocument> documents = store.getDatabaseCommands().getDocuments(0, 10, false);

StartsWith

StartsWith can be used to retrieve multiple documents for a specified key prefix.

Syntax

public List<JsonDocument> startsWith(String keyPrefix, String matches, int start, int pageSize);

public List<JsonDocument> startsWith(String keyPrefix, String matches, int start, int pageSize, boolean metadataOnly);

public List<JsonDocument> startsWith(String keyPrefix, String matches, int start, int pageSize, boolean metadataOnly, String exclude);

public List<JsonDocument> startsWith(String keyPrefix, String matches, int start, int pageSize, boolean metadataOnly, String exclude, RavenPagingInformation pagingInformation);

public List<JsonDocument> startsWith(String keyPrefix, String matches, int start, int pageSize, boolean metadataOnly,
  String exclude, RavenPagingInformation pagingInformation, String transformer, Map<String, RavenJToken> transformerParameters);

public List<JsonDocument> startsWith(String keyPrefix, String matches, int start, int pageSize, boolean metadataOnly,
  String exclude, RavenPagingInformation pagingInformation, String transformer, Map<String, RavenJToken> transformerParameters, String skipAfter);
Parameters
keyPrefix String prefix for which documents should be returned
matches String pipe ('|') separated values for which document keys (after 'keyPrefix') should 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
pagingInformation RavenPagingInformation used to perform rapid pagination on a server side (default: null)
metadataOnly boolean specifies if only document metadata should be returned (default : false)
exclude String pipe ('|') separated values for which document keys (after 'keyPrefix') should not be matched ('?' any single character, '*' any characters) (default: null)
transformer String name of a transformer that should be used to transform the results (default: null)
transformerParameters Map<String, RavenJToken> inputs (parameters) that will be used by transformer (default: null)
skipAfter String skip document fetching until given key is found and return documents after that key (default: null)
Return Value
JsonDocument Object representing retrieved document.

Example I

// return up to 128 documents with key that starts with 'products'
List<JsonDocument> result = store.getDatabaseCommands().startsWith("products", null, 0, 128);

Example II

// return up to 128 documents with key that starts with 'products/'
// and rest of the key begins with "1" or "2" e.g. products/10, products/25
List<JsonDocument> result = store.getDatabaseCommands().startsWith("products/", "1*|2*", 0, 128);

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
List<JsonDocument> result = store.getDatabaseCommands().startsWith("products/", "1?1", 0, 128);