Azure Cosmos Document DB - Read multiple documents by id

Refresh

April 2019

Views

924 time

1

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?

1 answers

3

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.