Pradeep Vishwakarma
Pradeep Vishwakarma

Reputation: 11

How to join two collections in document db using c#

I have Document db and I have a scenario where I'm going to join two collections for required result, but I'm not able to join two collections.

This is the simple select query to get records:

SELECT 
c.DocumentName,
c.DocumentID,
c.VersionID,
c.VersionName,
c.PageID,
c.OCRSpan 
FROM c 
where ARRAY_CONTAINS([4780,4779], c.VersionID)
var endpoint = ConfigurationManager.AppSettings["DocDbEndpoint"];
var masterKey = ConfigurationManager.AppSettings["DocDbMasterKey"];

using (var client = new DocumentClient(new Uri(endpoint), masterKey))
{
    FeedOptions queryOption = new FeedOptions { MaxItemCount = 100 };
    int[] ids = { 4779 };

    //Execute Store Procedure                    
    var result = await client.ExecuteStoredProcedureAsync<string>("dbs/HOCRData/colls/HOCR/sprocs/getData/", new RequestOptions() { PartitionKey = new PartitionKey(Undefined.Value) }, ids);

    Console.WriteLine($" {result.Response} ");                    
    Console.ReadKey();
    ...

The code mentioned is only for a single collection, but I'm expecting to join two collections together.

Or is there any alternate way to join two collection documents?

Upvotes: 1

Views: 5458

Answers (1)

Chris Anderson
Chris Anderson

Reputation: 8515

You cannot join collections in Cosmos DB.

As for alternates, you have a few options that all require some significant changes:

  1. Put all your data into one collection. Then you can do joins.
  2. Keep your collections separate and do the join locally via code.
  3. Avoid joins all together by denormalizing your data to avoid the need to do joins.

Upvotes: 4

Related Questions