nightroman / Mdbc

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

unable to use -options in update-mdbcdata #65

Closed swedan333 closed 3 years ago

swedan333 commented 3 years ago
$arrayFilters = New-Object System.Collections.Generic.List[MongoDB.Driver.ArrayFilterDefinition]
[MongoDB.Driver.BsonDocumentArrayFilterDefinition]$credentialId = [MongoDB.Bson.BsonDocument]::new('credential.guid', [MongoDB.Bson.BsonDocument]::new('$eq', '8ab9d46a-7af4-46bc-96c3-b98529af1a95'))
$arrayFilters.Add($credentialId) 

$mOptions = [MongoDB.Driver.UpdateOptions]::new()
$mOptions.ArrayFilters = $arrayFilters

update-mdbcdata -Filter @{} -Collection $colCompany -Update @{'$addToSet' = @{'credential.$[credential].test' = $dataObj}} -Options $mOptions -Result

is there a right way of doing this?

nightroman commented 3 years ago

What is the Mdbc version? What is the error? What are you trying to do? It looks like you build a filter but use it as -Options instead of -Filter, it's not clear.

nightroman commented 3 years ago

This code runs without errors. But I am not sure it does what you need.

$arrayFilters = New-Object System.Collections.Generic.List[MongoDB.Driver.ArrayFilterDefinition]
$filter = [MongoDB.Bson.BsonDocument]::new(@{
    'credential.guid' = @{
        '$eq' = '8ab9d46a-7af4-46bc-96c3-b98529af1a95'
    }
})
[MongoDB.Driver.BsonDocumentArrayFilterDefinition[object]]$credentialId = $filter
$arrayFilters.Add($credentialId)

$mOptions = [MongoDB.Driver.UpdateOptions]::new()
$mOptions.ArrayFilters = $arrayFilters

Update-MdbcData -Filter @{} -Collection $colCompany -Update @{'$addToSet' = @{'credential.$[credential].test' = $dataObj}} -Options $mOptions -Result
swedan333 commented 3 years ago

Hi, and thanks for the quick response! I'm running Mdbc version 6.5.10 and I am trying to update sub arrays using Arrayfilters, although the code ran without errors it still did not update the record.

swedan333 commented 3 years ago

Solved! Was the -filter as you suggested, thought I could update multiple. thanks for the proper arrayFilters implementation, maybe this should be documented..

nightroman commented 3 years ago

I am glad it's solved!

thanks for the proper arrayFilters implementation, maybe this should be documented..

I do not really know how and what to document in this case. It's just some expected (!) quirks of PowerShell on translating from C#. In a way, some of them can be found in Mdbc tests in the repo.