Closed codaamok closed 1 year ago
Class mapping is the advanced C# driver topic (and I'm not exactly an expert). But in your particular case values might be not "missing" but actual nulls. Can you check on your side?
For example, this script
Set-StrictMode -Version 3
$r = Get-PSDrive C
$r | Format-List * | Out-String
$null -eq $r.MaximumSize
$null -eq $r.DisplayRoot
shows that your properties are nulls in my case.
I'm mostly looking at the Name property more than anything.
PS C:\Users\WDAGUtilityAccount> $null -eq (get-item .\Downloads\mongodb-windows-x86_64-6.0.3-signed.msi).psdrive.name
False
PS C:\Users\WDAGUtilityAccount> $null -eq (get-psdrive 'c').name
False
Just seems like a regular string?
Well, this is a question to the C# driver class map makers then :) I do not know why Name
is not saved.
Name
only has get
, maybe they do not consider such properties suitable for saving.
Other saved properties have at least internal set
(used ILSpy for getting this).
I would not call this issue surprising though. We cannot expect than any type is suitable for mapping and that the default mapping is what we want.
Also, keep in mind that mapping is designed for two way serialization, to BSON first and then recreating from BSON. A property with get
only is "presumably calculated", so "there is no need to store it".
If you still want to go this route, then you need a custom mapping, read the C# driver manuals.
e.g. this map
Register-MdbcClassMap -Type ([System.Management.Automation.PSDriveInfo]) -Init {
$null = $_.MapProperty('Name')
$null = $_.MapProperty('Root')
}
will result in Name
and Root
saved.
Here is some old-ish tutorial https://mongodb.github.io/mongo-csharp-driver/1.11/serialization/
I appreciate your insight! Thank you š
I think this might work for you:
Register-MdbcClassMap -Type ([System.Management.Automation.PSDriveInfo]) -Init {
$_.AutoMap()
$null = $_.MapProperty('Name')
}
I will add this use case to tests, it's useful, thank you for the question.
Firstly, love this. Thank you. I am learning mongodb and being able to learn it with PowerShell helps me so much.
During my learning experience I uncovered Mdbc can't serialise all types, I must use
Register-MdbcClassMap
. Which is fine. As an example, I'm trying to run:I needed to register all of the following types in order to add the data:
After this, I was able to successfully add my System.IO.FileInfo object as data to my mongodb collection š
When looking at the data in the collection, some properties are missing, for example see below screenshot. Is there anything I can do to ensure all properties from other types/objects are added? I'll likely be throwing in Pester data in collections once I get comfortable with how to use.