title: Cross JOIN collections and GroupBy CosmosDB Javascript API
body: <p>I am searching for a solution in the Javascript API for CosmosDB, where you can perform an INNER/OUTER JOIN between two document collections.</p>
  
  <p>I have been unsuccessful.  </p>
  
  <p>From my understanding, Javascript Stored Procedures run within a collection, and cannot access/reference data in another collection.  </p>
  
  <p>If the above is true, where does this leave our application's datasource that has been designed in a relational way?  If Business requires a immediate query, to collect pthe following data:
  All agreements/contracts that has been migrated to a new product offering, within a specific region, for a given time frame.  How would I go about this query, if there are about 5 collections containing all infromation related to this query?</p>
  
  <p>Any guidance?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p><strong><em>Customer</em></strong></p>
  
  <pre><code>{
      "id": "d02e6668-ce24-455d-b241-32835bb2dcb5",
      "Name": "Test User One",
      "Surname": "Test"
  }
  </code></pre>
  
  <p><strong><em>Agreement</em></strong></p>
  
  <pre><code>{
      "id": "ee1094bd-16f4-45ec-9f5e-7ecd91d4e729",
      "CustomerId": "d02e6668-ce24-455d-b241-32835bb2dcb5"
      "RetailProductVersionInstance": 
      [
                  {
          "id": "8ce31e7c-7b1a-4221-89a3-449ae4fd6622",
          "RetailProductVersionId": "ce7a44a4-7e49-434b-8a51-840599fbbfbb",
          "AgreementInstanceUser": {
              "FirstName": "Luke",
              "LastName": "Pothier",
              "AgreementUserTypeId": ""
          },
          "AgreementInstanceMSISDN": {
              "IsoCountryDialingCode": null,
              "PhoneNumber": "0839263922",
              "NetworkOperatorId": "30303728-9983-47f9-a494-1de853d66254"
          },
          "RetailProductVersionInstanceState": "IN USE",
          "IsPrimaryRetailProduct": true,
          "RetailProductVersionInstancePhysicalItems": [
              {
                  "id": "f8090aba-f06b-4233-9f9e-eb2567a20afe",
                  "PhysicalItemId": "75f64ab3-81d2-f600-6acb-d37da216846f",
                  "RetailProductVersionInstancePhysicalItemNumbers": [
                      {
                          "id": "9905058b-8369-4a64-b9a5-e17e28750fba",
                          "PhysicalItemNumberTypeId": "39226b5a-429b-4634-bbce-2213974e5bab",
                          "PhysicalItemNumberValue": "KJDS959405"
                      },
                      {
                          "id": "1fe09dd2-fb8a-49b3-99e6-8c51df10adb1",
                          "PhysicalItemNumberTypeId": "960a1750-64be-4333-9a7f-c8da419d670a",
                          "PhysicalItemNumberValue": "DJDJ94943"
                      }
                  ],
                  "RetailProductVersionInstancePhysicalItemState": "IN USE",
                  "DateCreatedUtc": "2018-11-21T13:55:00Z",
                  "DateUpdatedUtc": "2020-11-21T13:55:00Z"
              }
          ]
      }
      ]
  }
  </code></pre>
  
  <p><strong><em>RetailProduct</em></strong></p>
  
  <pre><code>{
      "id": "ce7a44a4-7e49-434b-8a51-840599fbbfbb",
      "FriendlyName": "Data-Package 100GB",
      "WholeSaleProductId": "d054dae5-173d-478b-bb0e-7516e6a24476"
  }
  </code></pre>
  
  <p><strong><em>WholeSaleProduct:</em></strong> </p>
  
  <pre><code>{
      "id": "d054dae5-173d-478b-bb0e-7516e6a24476",
      "ProductName": "Data 100",
      "ProviderLiabilities": []
  }
  </code></pre>
  
  <p>Above, I have added some sample documentation.  </p>
  
  <p>Relationships:</p>
  
  <ul>
  <li>Agreement.CustomerId links to Customer.id</li>
  <li>Agreement.RetailProductVersionInstance.RetailProductVersionId links
  to RetailProduct.id</li>
  <li>RetailProduct.WholeSaleProductId links to WholeSaleProduct.id</li>
  </ul>
  
  <p>How, would I write a Javascript Stored Procedure, in CosmosDB, to perform joins between these 4 collections?</p>