PlagueHO / CosmosDB

PowerShell Module for working with Azure Cosmos DB databases, collections, documents, attachments, offers, users, permissions, triggers, stored procedures and user defined functions.
http://dscottraynsford.com
MIT License
154 stars 46 forks source link

How can I obtain the JSON of a document given the item id? #370

Closed sdg002 closed 4 years ago

sdg002 commented 4 years ago

Issue

Before submitting your issue for the CosmosDB project, please take a moment to provide the following details:

Scenario

I would like to dump documents from my collection using a query into a physical folder. I would like to make some minor changes and then save them back.

Get-CosmosDbDocument

This worked well. But, it gave me a tabular result set - which is also very useful in other scenarios.

Thanks, Sau

PlagueHO commented 4 years ago

Hi @sdg002, do you want to just return the RAW JSON of the document, rather than the document object?

You could just convert that to json using ConvertTo-Json - e.g. Get-CosmosDbDocument -Context $cosmosDbContext -CollectionId 'MyNewCollection' -Id $documents[0].id -PartitionKey $documents[0].id | ConvertTo-Json -Depth 20.

However, I'm assuming you're probably wanting the document "exactly" as it is returned from Cosmos DB - is that correct? In which case I could easily implement a -Raw parameter that would just return the RAW json - would that address your requirement?

sdg002 commented 4 years ago

A parameter like -Raw might work well.

Or, a new purpose built cmdlet like Get-CosmosDbDocumentJson so that the intent is very clear to the consumer and there is no confusion with Get-CosmosDbDocument which is already doing a great job.

PlagueHO commented 4 years ago

Great Idea - I'll use the Get-CosmosDbDocumentJson. What I can do is actually just make Get-CosmosDbDocument a wrapper around Get-CosmosDbDocumentJson which will eliminate any code duplication.

I'll get onto this later today.