nightroman / Mdbc

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

Cant update array with update-mdbcdata #50

Closed jonatanhost closed 3 years ago

jonatanhost commented 3 years ago

I try to update value in a array of a document.

Update-MdbcData : A write operation resulted in an error. Cannot create field 'Updated' in array

The document I have in MongoDB is like below and I want to update value "Updated" for "arrayId" 2.

{ "_id": "", "array": [ { "arrayId": "1", "Updated": "201212" }, { "arrayId": "2", "Updated": "201210" } ] }

nightroman commented 3 years ago

What is your full command with parameters that gives this error?

nightroman commented 3 years ago

Just in case, the error is about LastUpdated but you want to change Updated. Typo?

jonatanhost commented 3 years ago

That was a typo.

I tried with this command and structure below

Update-MdbcData @{arrayId=1} @{'$set' =@{"array.Updated" = "201212"}}

nightroman commented 3 years ago

https://stackoverflow.com/a/30059057/323582

# add test data
Connect-Mdbc -NewCollection
@{_id = ""; array = @( @{ arrayId = "1"; Updated = "201212" }, @{ arrayId = "2"; Updated = "201210" } ) } | Add-MdbcData

# update
Update-MdbcData @{'array.arrayId' = '1'} @{'$set' = @{'array.$.Updated' = "new-value"}}

# { "_id" : "", "array" : [{ "arrayId" : "1", "Updated" : "new-value" }, { "arrayId" : "2", "Updated" : "201210" }] }
(Get-MdbcData).ToString()