I'm trying to query a Cosmos Database to find out the ids of all of its containers.
Using the below code it gives me a json response that is a single item (not an array).
Is there a way to write the query so that I only get an array of the 'id' fields from the DocumentCollection. e.g. something like
[ { "id": "Summary-v00019"}, {"id": "Details-V00019"} ]
using Microsoft.Azure.Cosmos;
using System;
using System.IO;
namespace QueryDatabase
{
class Program
{
const string CosmosConnectionString = "AccountEndpoint=https://some-cosmos-ccount.documents.azure.com:443/;AccountKey=blahblah==;";
const string Database = "database-name";
static async System.Threading.Tasks.Task Main()
{
var cosmosClient = new CosmosClient(CosmosConnectionString);
var database = cosmosClient.GetDatabase(Database);
string queryText = "SELECT * FROM c";
QueryDefinition queryDefinition = new QueryDefinition(queryText);
FeedIterator feedIterator = database.GetContainerQueryStreamIterator(queryDefinition);
while (feedIterator.HasMoreResults)
{
using (ResponseMessage response = await feedIterator.ReadNextAsync())
{
using (var sr = new StreamReader(response.Content))
{
var text = sr.ReadToEnd();
Console.WriteLine(text);
}
}
}
}
}
}
JSON Response from above code:
{
"_rid": "blah==",
"DocumentCollections": [
{
"id": "Summary-v00019",
"indexingPolicy": {
"indexingMode": "consistent",
"automatic": true,
"includedPaths": [
{
"path": "/*"
}
],
"excludedPaths": [
{
"path": "/\"_etag\"/?"
}
]
},
"partitionKey": {
"paths": [
"/foo"
],
"kind": "Hash"
},
"conflictResolutionPolicy": {
"mode": "LastWriterWins",
"conflictResolutionPath": "/_ts",
"conflictResolutionProcedure": ""
},
"geospatialConfig": {
"type": "Geography"
},
"_rid": "blah=",
"_ts": 1591935021,
"_self": "dbs/blah==/colls/blah=/",
"_etag": "\"blah-blah-blah-0000-blah\"",
"_docs": "docs/",
"_sprocs": "sprocs/",
"_triggers": "triggers/",
"_udfs": "udfs/",
"_conflicts": "conflicts/"
},
{
"id": "Details-v00019",
"indexingPolicy": {
"indexingMode": "none",
"automatic": false,
"includedPaths": [],
"excludedPaths": []
},
"partitionKey": {
"paths": [
"/bar"
],
"kind": "Hash"
},
"conflictResolutionPolicy": {
"mode": "LastWriterWins",
"conflictResolutionPath": "/_ts",
"conflictResolutionProcedure": ""
},
"geospatialConfig": {
"type": "Geography"
},
"_rid": "blah=",
"_ts": 1591935021,
"_self": "dbs/blah==/colls/blah=/",
"_etag": "\"blah-0000-blah-0000-blah\"",
"_docs": "docs/",
"_sprocs": "sprocs/",
"_triggers": "triggers/",
"_udfs": "udfs/",
"_conflicts": "conflicts/"
}
],
"_count": 2
}
"SELECT c.DocumentCollections.id FROM c".