CosmosDB icon indicating copy to clipboard operation
CosmosDB copied to clipboard

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

Open sdg002 opened this issue 5 years ago • 2 comments

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:

  • [ 5.1] Version of PowerShell you're using
  • [ ISE] PowerShell host you're using (eg. Console Host, ISE, Visual Studio)
  • [ Windows 10] Operating system you're running
  • [ 4.2.1] Version of CosmosDB PowerShell Module you're using (use Get-Module -Name CosmosDB)

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.

sdg002 avatar Jun 28 '20 16:06 sdg002

$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

tamusjroyce avatar Jul 10 '20 03:07 tamusjroyce

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?

PlagueHO avatar Jul 16 '20 06:07 PlagueHO