nightroman / Mdbc

MongoDB Cmdlets for PowerShell
Apache License 2.0
141 stars 16 forks source link

Selecting field using Get-MdbcData #52

Closed kipster521 closed 3 years ago

kipster521 commented 3 years ago

I would like to select a field (styleType) from a collection and return its values into a file. This is what my code looks like: Get-MdbcData -Collection $MyColl -Project styleType | Export-MdbcData $Path -Append The error message I received is: JSON reader was expecting a value but found 'styleType'. Is Project not the correct parameter for selecting fields?

nightroman commented 3 years ago

What type is styleType? Before digging into the code (I do not remember :)), maybe Export-MdbcData expects objects but finds scalars.

kipster521 commented 3 years ago

styleType is a String. And the error was on the Get-MdbcData.

nightroman commented 3 years ago

You should use a projection expression, something like "{styleType: 1}" or @{styleType=1}.

0> help Get-MdbcData -Parameter Project

-Project
    Specifies the projection expression, i.e. fields to be retrieved. The field _id is always included unless it is explicitly excluded.

    The special value `*` used together with `-As <type>` tells to infer projected fields from the type.

    Otherwise, the argument is either JSON or similar dictionary.

See also: https://docs.mongodb.com/manual/tutorial/project-fields-from-query-results/

kipster521 commented 3 years ago

Below is the updated code that worked. Thanks for your assistance. Get-MdbcData -Project @{styleType=1; _id=0} | Export-MdbcData $Path -Append