phpazureazure-cosmosdb

Query Records Across Multiple Collections in Azure Cosmos


Is it possible to query records across multiple collections in Azure Cosmos, using the SQL Rest API?

I'll note that I am using jupitern/cosmosdb, which is just a handy wrapper around the SQL Rest API. I have not been able to find any documentation on Microsoft's website indicating that's its possible, but I'm hoping someone in the community might know differently.

I get the sense my only course here, is to query one collection at a time, and compile the results on my end, based on the fact that the Query Documents documentation, demonstrates that the URI requires the specific collection ID as a parameter:

https://{databaseaccount}.documents.azure.com/dbs/{db-id}/colls/{coll-id}/docs

Here's a sample of how I'm currently querying Cosmos, using that wrapper library:

# connect to cosmos
$conn = new Cosmos(AZURE_COSMOS_HOST, AZURE_COSMOS_KEY);
$conn->setHttpClientOptions(['verify' => false]);
$db = $conn->selectDB("Example");
$coll = $db->selectCollection("Test");

# query records
try {
    $res = Query::instance()
        ->setCollection($coll)
        ->select("*")
        ->findAll()
        ->toArray();

} catch (Exception $e) {
    return $response->withJson($e->getMessage());
}

Solution

  • Is it possible to query records across multiple collections in Azure Cosmos, using the SQL Rest API?

    Simple answer to your question is no. It is not possible to query records across multiple collections. You will need to issue separate queries against each collection individually and work with the results on the client side.