Skip to main content

Commands: Get Documents

Get single document

GetDocumentsCommand can be used to retrieve a single document

Syntax:

public GetDocumentsCommand(string id, string[] includes, bool metadataOnly)
ParametersTypeDescription
idstringID of the documents to get
includesstring[]Related documents to fetch along with the document
metadataOnlybooleanWhether to fetch the whole document or just the metadata.

Example:

var command = new GetDocumentsCommand("orders/1-A", null, false);
session.Advanced.RequestExecutor.Execute(command, session.Advanced.Context);
var order = (BlittableJsonReaderObject)command.Result.Results[0];

Get multiple documents

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

Syntax:

public GetDocumentsCommand(string[] ids, string[] includes, bool metadataOnly)
ParametersTypeDescription
idsstring[]IDs of the documents to get
includesstringRelated documents to fetch along with the documents
metadataOnlybooleanWhether to fetch whole documents or just the metadata

Example I

var command = new GetDocumentsCommand(new[] { "orders/1-A", "employees/3-A" }, null, false);
session.Advanced.RequestExecutor.Execute(command, session.Advanced.Context);
var order = (BlittableJsonReaderObject)command.Result.Results[0];
var employee = (BlittableJsonReaderObject)command.Result.Results[1];

Example II - Using Includes

// Fetch employees/5-A and his boss.
var command = new GetDocumentsCommand("employees/5-A", includes: new[] { "ReportsTo" }, metadataOnly: false);
session.Advanced.RequestExecutor.Execute(command, session.Advanced.Context);
var employee = (BlittableJsonReaderObject)command.Result.Results[0];
if (employee.TryGet<string>("ReportsTo", out var bossId))
{
var boss = command.Result.Includes[bossId];
}

Example III - Missing Documents

// Assuming that products/9999-A doesn't exist.
var command = new GetDocumentsCommand(new[] { "products/1-A", "products/9999-A", "products/3-A" }, null, false);
session.Advanced.RequestExecutor.Execute(command, session.Advanced.Context);
var products = command.Result.Results; // 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)
ParametersTypeDescription
startintnumber of documents that should be skipped
pageSizeintmaximum number of documents that will be retrieved

Example:

var command = new GetDocumentsCommand(start: 0, pageSize: 128);
session.Advanced.RequestExecutor.Execute(command, session.Advanced.Context);
var firstDocs = command.Result.Results;

Get documents by ID prefix

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, bool metadataOnly)
ParametersTypeDescription
startsWithstringprefix for which documents should be returned
startAfterstringskip 'document fetching' until the given ID is found, and return documents after that ID (default: null)
matchesstringpipe ('|') separated values for which document IDs (after startsWith) should be matched ('?' any single character, '*' any characters)
excludestringpipe ('|') separated values for which document IDs (after startsWith) should not be matched ('?' any single character, '*' any characters)
startintnumber of documents that should be skipped
pageSizeintmaximum number of documents that will be retrieved
metadataOnlyboolspecifies whether or not only document metadata should be returned

Example I

// return up to 128 documents with key that starts with 'products'
var command = new GetDocumentsCommand(
startWith: "products",
startAfter: null,
matches: null,
exclude: null,
start: 0,
pageSize: 128,
metadataOnly: false);
session.Advanced.RequestExecutor.Execute(command, session.Advanced.Context);
var products = command.Result.Results;

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
var command = new GetDocumentsCommand(
startWith: "products",
startAfter: null,
matches: "1*|2*",
exclude: null,
start: 0,
pageSize: 128,
metadataOnly: false);
session.Advanced.RequestExecutor.Execute(command, session.Advanced.Context);
var products = command.Result.Results;

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
var command = new GetDocumentsCommand(
startWith: "products",
startAfter: null,
matches: "1?1",
exclude: null,
start: 0,
pageSize: 128,
metadataOnly: false);
session.Advanced.RequestExecutor.Execute(command, session.Advanced.Context);
var products = command.Result.Results;