Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Cosmos Document DB - Read multiple documents by id

I'm using Azure Cosmos Document DB and want to fetch several documents in one call.

I've understood that the best way of retrieving a document if you have its id is to use DocumentClient.ReadDocumentAsync(...)

I wonder if there is a similar way to fetch multiple documents? Right now I'm doing a DocumentClient.CreateDocumentQuery<T>(...).Where(document => ids.Contains(document.id))

Does anyone know if this is the preferred way to do this or is there another way?

like image 238
OriginalUtter Avatar asked Mar 15 '18 15:03

OriginalUtter


People also ask

What is Cosmos DB in azure?

Azure Cosmos DB is a globally distributed multi-model database that supports the document, graph, and key-value data models. The content in this section is for creating, querying, and managing document resources using the SQL API via REST. The document resource is represented by docs in the Azure Cosmos DB resource model.

What is readdocumentasync in Cosmos DB?

ReadDocumentAsync(Uri, RequestOptions, CancellationToken) Reads a Document as an asynchronous operation from the Azure Cosmos DB service. Reads a Document as a generic type T from the Azure Cosmos DB service as an asynchronous operation.

How do I read a resource from a Cosmos DB service?

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. Reads a Document as a generic type T from the Azure Cosmos DB service as an asynchronous operation. The link for the document to be read.

Does Cosmos DB require any schema or secondary indexes?

Azure Cosmos DB does not require any schema or secondary indexes in order to support querying over documents in a collection. By default, documents are automatically and indexed in a consistent manner, thus making a document queryable as soon as it is created. Documents are stored within collections.


1 Answers

Your second approach with CreateDocumentQuery works well. If you are using a partitioned collection, and querying cross-partition, and have a unique per-document partition key as well as an id, I have observed better performance constraining on the partition key instead of the id even though both are unique. If your id is also your partition key, this should just work with no special effort.

If you are retrieving many documents, you will need to divide your ids collection into batches to avoid length limits of the generated SQL expressions (the long IN statement). I use batches of 500 in a current project, but it depends on the length of your ids. You can issue multiple batches in parallel.

like image 84
Jason Avatar answered Sep 24 '22 15:09

Jason