2
votes

Created a Collection in Cosmos DB with Email as partition key and added around ten documents to that collection.

enter image description here


When I query the collection with Partition Key in RequestOptions, returns all the documents which has the supplied partition key.

var result = await documentRUClient.ExecuteStoredProcedureAsync<string>
(UriFactory.CreateStoredProcedureUri(DataBaseId, CollectionId, 
"GetCollection"), new RequestOptions { PartitionKey = 
new PartitionKey("test @test.com") });

Stored Procedure:

function GetCollection() {
    var context = getContext();
    var collectionManager = context.getCollection();        
    var collectionLink = collectionManager.getSelfLink();

    var query = "SELECT * FROM c";

    collectionManager.queryDocuments(collectionLink, query, function(err, documents){
        context.getResponse().setBody(documents);
    });
}

Now I need to get all the documents from all partition in that collection. I have tried the below code, but it gives the error PartitionKey value must be supplied for this operation.

 var result = await documentRUClient.ExecuteStoredProcedureAsync<string>
    (UriFactory.CreateStoredProcedureUri(DataBaseId, CollectionId, 
    "GetCollection"));

Please suggest a way to achieve this.

1

1 Answers

3
votes

It is a limitation of the Stored procedure in Cosmos. It can execute only within the partition.

Below is the extract from the MSDN

If the collection the stored procedure is registered against is a single-partition collection, then the transaction is scoped to all the documents within the collection. If the collection is partitioned, then stored procedures are executed in the transaction scope of a single partition key.

You have to use CreateDocumentQuery and setting the EnableCrossPartitionQuery as true in the FeedOptions to get all the document in the collection.