

Cosmos DB – Retrieve all attributes/fields from the JSON document of all Collect...
source link: https://sqlwithmanoj.com/2021/04/06/cosmos-db-retrieve-all-attributes-fields-from-the-json-document-of-all-collections-using-net/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Cosmos DB – Retrieve all attributes/fields from the JSON document of all Collections (using .net)
In our [previous post] we saw how to get COUNT of items/records from all Collections in all Databases of a CosmosDB instance by programmatically/dynamically iterating over all these objects.
Here in this post we will extend it and see how to retrieve all attributes/fields from the JSON document/items present in these Collections. (This is some work I’m doing for maintaining a data catalogue and data classification for privacy and compliance purpose.)
On Azure portal go to your Azure Cosmos DB instance, from Overview tab go to Keys tab and copy the “URI” & “PRIMARY READ-ONLY KEY” key values in code below.
We have discussed all the methods used below in our [previous post], please check here. As the Cosmos DB SQL container is a schema-agnostic and the items in a container can have arbitrary schemas, thus we will read the first JSON document/item from the Collection by using FirstOrDefault() method and Deserialize this JSON document Object to a Dictionary, and finally read all the attributes from this Dictionary Keys:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
Microsoft.Azure.Documents;
using
Microsoft.Azure.Documents.Client;
using
Newtonsoft.Json;
using
Newtonsoft.Json.Linq;
namespace
CosmosDBgetJSONAttributes
{
class
Program
{
static
void
Main(
string
[] args)
{
string
PK =
"YourPrimaryKey=="
;
DocumentClient cosmosClient =
new
DocumentClient(
new
Uri(EndPointUri), PK);
string
sqlQuery =
"SELECT * FROM d"
;
var
dbs = cosmosClient.CreateDatabaseQuery(sqlQuery).ToList();
foreach
(
var
db
in
dbs)
{
Console.WriteLine(
"Database: "
+ db.id);
List<DocumentCollection> collections = cosmosClient.CreateDocumentCollectionQuery((String)db._self).ToList();
foreach
(
var
collection
in
collections)
{
Console.WriteLine(
" - Collection: "
+ collection.Id);
var
docu = cosmosClient.CreateDocumentQuery(collection.SelfLink, $
"SELECT TOP 1 * FROM c"
,
new
FeedOptions() { EnableCrossPartitionQuery =
true
, MaxItemCount = 1, })
.AsEnumerable().FirstOrDefault();
if
(docu !=
null
)
{
var
docuStr = Convert.ToString(docu);
var
data = JsonConvert.DeserializeObject<Dictionary<
string
,
string
>>(docuStr);
foreach
(
var
item
in
data.Keys)
{
Console.WriteLine(
" - Attribute: "
+ item);
}
}
else
{
Console.WriteLine(
" - Attribute: no data"
);
}
}
Console.WriteLine(Environment.NewLine);
}
Console.WriteLine(
"Finished reading all Collections attributes"
);
Console.Read();
}
}
}
Related
Recommend
-
197
-
75
GitHub is where people build software. More than 28 million people use GitHub to discover, fork, and contribute to over 80 million projects.
-
53
README.org gscholar bibtex
-
7
Small Bytes of MongoDB (4 Part Series) For a full overview of MongoDB and all my posts on it, check out my
-
7
Larger document sizes, unique index improvements, $expr support in Azure Cosmos DB API for MongoDB Shweta N April 20th, 2022 We are exc...
-
7
Scenario Using the alerting plugin within Opensearch, you’d like to include information about the document which triggered an alert. For example, including a field within the email that is sent out as a result of the alert being trigge...
-
10
Closed Bug 1785218...
-
4
Abeeha Rizvi November 23, 2022 Less than a 1 minute read...
-
12
In a previous tutorial, you learned how to create your first Angular application using the TX Text Control Document Editor....
-
9
Drag and Drop Merge Fields into the Document Editor For many applications, the out-of-the-box Reporting UI may be too complex and require too much training. It is helpful to tailor the UI to the user's level...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK