see on GitHub

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

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.DatabaseCommands.Get("products/1"); // null if does not exist

Get - multiple documents

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

Syntax

MultiLoadResult Get(
	string[] ids,
	string[] includes,
	string transformer = null,
	Dictionary<string, RavenJToken> transformerParameters = null,
	bool metadataOnly = false);

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
transformerParameters Dictionary<string, RavenJToken> parameters that will be passed to transformer
metadataOnly bool specifies if only document metadata should be returned

public class MultiLoadResult
{
	public List<RavenJObject> Results { get; set; }

	public List<RavenJObject> Includes { get; set; }
}

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
	.DatabaseCommands
	.Get(new[] { "products/1", "products/2" }, null);

Example II - using includes

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

List<RavenJObject> results = resultsWithIncludes.Results; // products/1, products/2
List<RavenJObject> includes = resultsWithIncludes.Includes; // categories/1

Example III - missing documents

// assuming that 'products/9999' does not exist
MultiLoadResult resultsWithIncludes = store
	.DatabaseCommands
	.Get(
		new[] { "products/1", "products/9999", "products/3" },
		null);

List<RavenJObject> results = resultsWithIncludes.Results; // products/1, null, products/3
List<RavenJObject> includes = resultsWithIncludes.Includes; // empty

GetDocuments

GetDocuments can be used to retrieve multiple documents.

Syntax

JsonDocument[] GetDocuments(int start, int pageSize, bool metadataOnly = false);

Parameters
start int number of documents that should be skipped
pageSize int maximum number of documents that will be retrieved
metadataOnly bool specifies if only document metadata should be returned
Return Value
JsonDocument Object representing retrieved document.

Example

JsonDocument[] documents = store.DatabaseCommands.GetDocuments(0, 10, metadataOnly: false);

StartsWith

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

Syntax

JsonDocument[] StartsWith(
	string keyPrefix, 
	string matches,
	int start,
	int pageSize,
	RavenPagingInformation pagingInformation = null,
	bool metadataOnly = false,
	string exclude = null,
	string transformer = null,
	Dictionary<string, RavenJToken> transformerParameters = null,
	string skipAfter = null);

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
metadataOnly bool specifies if only document metadata should be returned
exclude string pipe ('|') separated values for which document keys (after 'keyPrefix') should not be matched ('?' any single character, '*' any characters)
transformer string name of a transformer that should be used to transform the results
transformerParameters Dictionary<string, RavenJToken> parameters that will be passed to transformer
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'
JsonDocument[] result = store.DatabaseCommands.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
JsonDocument[] result = store.DatabaseCommands.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
JsonDocument[] result = store.DatabaseCommands.StartsWith("products/", "1?1", 0, 128);