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

"select COL1,COL2 from docs c" does not give the display the results (but appears to work) #391

Open sdg002 opened 4 years ago

sdg002 commented 4 years ago

Issue

What works?

This query will give me all the fields in my document.

select * from docs c

What does not work as expected?

I would like to do selective querying.

$query="select c.id, c.FirstName,c.LastName from docs c"
$results=Get-CosmosDbDocument -context $cosmosDbContext -CollectionId $collnParticipants -Query $query -QueryEnableCrossPartition $true  -Verbose

I cannot see the FistName and LastName fields in the $results object. However, when I do $results[0].FirstName, I can see the value of the FirstName field .

Any ideas why this could be happening?

Thank you so much. Sau

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

Thanks for contributing your feedback and support! You can optionally submit a Pull Request against this project, if you have a fix you'd like to share.

tamusjroyce commented 4 years ago
$query="select c.id, c.FirstName,c.LastName from docs c"
$results=Get-CosmosDbDocument -context $cosmosDbContext -CollectionId $collnParticipants -Query $query -QueryEnableCrossPartition $true  -Verbose

$documents = $results | Foreach-Object {
    # Finds filter to filters out internal powershell-specific PSObject fields
    $docProperties = $_ | Get-Member -MemberType NoteProperty | ForEach-Object { $_.Name }
    # Applies filter, converts to json, then back to PSObject without unnecessary properties
    $doc = $_ | Select-Object -Property $docProperties | ConvertTo-Json | ConvertFrom-Json
}
$documents[0]

It took me a while to discover this. https://github.com/PlagueHO/CosmosDB/issues/382

PlagueHO commented 4 years ago

Hi @sdg002 - the Get-CosmosDbDocument function will return an array if more than one document is returned. In your case it does look like it could return more than one document. Can you confirm if this is the case?