Share via


IDocumentClient.ReadDocumentAsync Method

Definition

Overloads

ReadDocumentAsync(String, RequestOptions, CancellationToken)

Reads a Document from the Azure Cosmos DB service as an asynchronous operation.

ReadDocumentAsync(Uri, RequestOptions, CancellationToken)

Reads a Document as an asynchronous operation from the Azure Cosmos DB service.

ReadDocumentAsync<T>(String, RequestOptions, CancellationToken)

Reads a Document as a generic type T from the Azure Cosmos DB service as an asynchronous operation.

ReadDocumentAsync<T>(Uri, RequestOptions, CancellationToken)

Reads a Document as a generic type T from the Azure Cosmos DB service as an asynchronous operation.

ReadDocumentAsync(String, RequestOptions, CancellationToken)

Reads a Document from the Azure Cosmos DB service as an asynchronous operation.

public System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Document>> ReadDocumentAsync (string documentLink, Microsoft.Azure.Documents.Client.RequestOptions options = default, System.Threading.CancellationToken cancellationToken = default);
abstract member ReadDocumentAsync : string * Microsoft.Azure.Documents.Client.RequestOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Document>>
Public Function ReadDocumentAsync (documentLink As String, Optional options As RequestOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task(Of ResourceResponse(Of Document))

Parameters

documentLink
String

The link for the document to be read.

options
RequestOptions

(Optional) The request options for the request.

cancellationToken
CancellationToken

(Optional) A CancellationToken that can be used by other objects or threads to receive notice of cancellation.

Returns

A System.Threading.Tasks containing a ResourceResponse<TResource> which wraps a Document containing the read resource record.

Exceptions

If documentLink is not set.

This exception can encapsulate many different types of errors. To determine the specific error always look at the StatusCode property. Some common codes you may get when creating a Document are:

StatusCodeReason for exception
404NotFound - This means the resource you tried to read did not exist.
429TooManyRequests - This means you have exceeded the number of request units per second. Consult the DocumentClientException.RetryAfter value to see how long you should wait before retrying this operation.

Examples

//This reads a document record from a database & collection where
// - sample_database is the ID of the database
// - sample_collection is the ID of the collection
// - document_id is the ID of the document resource
var docLink = "dbs/sample_database/colls/sample_collection/docs/document_id";
Document doc = await client.ReadDocumentAsync(docLink);

Remarks

Doing a read of a resource is the most efficient way to get a resource from the Database. If you know the resource's ID, do a read instead of a query by ID.

The example shown uses ID-based links, where the link is composed of the ID properties used when the resources were created. You can still use the SelfLink property of the Document if you prefer. A self-link is a URI for a resource that is made up of Resource Identifiers (or the _rid properties). ID-based links and SelfLink will both work. The format for documentLink is always "dbs/{db identifier}/colls/{coll identifier}/docs/{doc identifier}" only the values within the {} change depending on which method you wish to use to address the resource.

See also

Applies to

ReadDocumentAsync(Uri, RequestOptions, CancellationToken)

Reads a Document as an asynchronous operation from the Azure Cosmos DB service.

public System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Document>> ReadDocumentAsync (Uri documentUri, Microsoft.Azure.Documents.Client.RequestOptions options = default, System.Threading.CancellationToken cancellationToken = default);
abstract member ReadDocumentAsync : Uri * Microsoft.Azure.Documents.Client.RequestOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.ResourceResponse<Microsoft.Azure.Documents.Document>>
Public Function ReadDocumentAsync (documentUri As Uri, Optional options As RequestOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task(Of ResourceResponse(Of Document))

Parameters

documentUri
Uri

A URI to the Document resource to be read.

options
RequestOptions

The request options for the request.

cancellationToken
CancellationToken

(Optional) A CancellationToken that can be used by other objects or threads to receive notice of cancellation.

Returns

A System.Threading.Tasks containing a ResourceResponse<TResource> which wraps a Document containing the read resource record.

Exceptions

If documentUri is not set.

This exception can encapsulate many different types of errors. To determine the specific error always look at the StatusCode property. Some common codes you may get when reading a Document are:

StatusCodeReason for exception
404NotFound - This means the resource you tried to read did not exist.
429TooManyRequests - This means you have exceeded the number of request units per second. Consult the DocumentClientException.RetryAfter value to see how long you should wait before retrying this operation.

Examples

//Reads a Document resource where 
// - db_id is the ID property of the Database
// - coll_id is the ID property of the DocumentCollection
// - doc_id is the ID property of the Document you wish to read. 
var docUri = UriFactory.CreateDocumentUri("db_id", "coll_id", "doc_id");
Document document = await client.ReadDocumentAsync(docUri);

Remarks

Doing a read of a resource is the most efficient way to get a resource from the service. If you know the resource's ID, do a read instead of a query by ID.

See also

Applies to

ReadDocumentAsync<T>(String, RequestOptions, CancellationToken)

Reads a Document as a generic type T from the Azure Cosmos DB service as an asynchronous operation.

public System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.DocumentResponse<T>> ReadDocumentAsync<T> (string documentLink, Microsoft.Azure.Documents.Client.RequestOptions options = default, System.Threading.CancellationToken cancellationToken = default);
abstract member ReadDocumentAsync : string * Microsoft.Azure.Documents.Client.RequestOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.DocumentResponse<'T>>
Public Function ReadDocumentAsync(Of T) (documentLink As String, Optional options As RequestOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task(Of DocumentResponse(Of T))

Type Parameters

T

Parameters

documentLink
String

The link for the document to be read.

options
RequestOptions

(Optional) The request options for the request.

cancellationToken
CancellationToken

(Optional) A CancellationToken that can be used by other objects or threads to receive notice of cancellation.

Returns

A System.Threading.Tasks containing a DocumentResponse<TDocument> which wraps a Document containing the read resource record.

Exceptions

If documentLink is not set.

This exception can encapsulate many different types of errors. To determine the specific error always look at the StatusCode property. Some common codes you may get when creating a Document are:

StatusCodeReason for exception
404NotFound - This means the resource you tried to read did not exist.
429TooManyRequests - This means you have exceeded the number of request units per second. Consult the DocumentClientException.RetryAfter value to see how long you should wait before retrying this operation.

Examples

//This reads a document record from a database & collection where
// - sample_database is the ID of the database
// - sample_collection is the ID of the collection
// - document_id is the ID of the document resource
var docLink = "dbs/sample_database/colls/sample_collection/docs/document_id";
Customer customer = await client.ReadDocumentAsync<Customer>(docLink);

Remarks

Doing a read of a resource is the most efficient way to get a resource from the Database. If you know the resource's ID, do a read instead of a query by ID.

The example shown uses ID-based links, where the link is composed of the ID properties used when the resources were created. You can still use the SelfLink property of the Document if you prefer. A self-link is a URI for a resource that is made up of Resource Identifiers (or the _rid properties). ID-based links and SelfLink will both work. The format for documentLink is always "dbs/{db identifier}/colls/{coll identifier}/docs/{doc identifier}" only the values within the {} change depending on which method you wish to use to address the resource.

See also

Applies to

ReadDocumentAsync<T>(Uri, RequestOptions, CancellationToken)

Reads a Document as a generic type T from the Azure Cosmos DB service as an asynchronous operation.

public System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.DocumentResponse<T>> ReadDocumentAsync<T> (Uri documentUri, Microsoft.Azure.Documents.Client.RequestOptions options = default, System.Threading.CancellationToken cancellationToken = default);
abstract member ReadDocumentAsync : Uri * Microsoft.Azure.Documents.Client.RequestOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Documents.Client.DocumentResponse<'T>>
Public Function ReadDocumentAsync(Of T) (documentUri As Uri, Optional options As RequestOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task(Of DocumentResponse(Of T))

Type Parameters

T

Parameters

documentUri
Uri

A URI to the Document resource to be read.

options
RequestOptions

The request options for the request.

cancellationToken
CancellationToken

(Optional) A CancellationToken that can be used by other objects or threads to receive notice of cancellation.

Returns

A System.Threading.Tasks containing a DocumentResponse<TDocument> which wraps a Document containing the read resource record.

Exceptions

If documentUri is not set.

This exception can encapsulate many different types of errors. To determine the specific error always look at the StatusCode property. Some common codes you may get when reading a Document are:

StatusCodeReason for exception
404NotFound - This means the resource you tried to read did not exist.
429TooManyRequests - This means you have exceeded the number of request units per second. Consult the DocumentClientException.RetryAfter value to see how long you should wait before retrying this operation.

Examples

//Reads a Document resource where 
// - db_id is the ID property of the Database
// - coll_id is the ID property of the DocumentCollection
// - doc_id is the ID property of the Document you wish to read. 
var docUri = UriFactory.CreateDocumentUri("db_id", "coll_id", "doc_id");
Customer customer = await client.ReadDocumentAsync<Customer>(docUri);

Remarks

Doing a read of a resource is the most efficient way to get a resource from the service. If you know the resource's ID, do a read instead of a query by ID.

See also

Applies to