nightroman / Mdbc

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

Use Get-MdbcData to explicitly exclude the _id field #86

Closed emnp88 closed 7 months ago

emnp88 commented 7 months ago

Hello!

Goal: compare mongodb document to a powershell hash table. The keys in the hash table are the same as the keys in the mongodb document. I want to compare the two. If there is a difference, update it with the key-value pairs in the hash table.

Problem: In my script, the $original_doc (found by using Get-Mdbc) and the $modified_doc (powershell hash table as converted from JSON file) will ALWAYS be different, so it will always be updated, regardless if the rest of the key-value pairs are the same or different

Here's what I get for $original_doc for example:

echo $original_doc

Name   Value
----    -----
_id   65de11cb8fc41c516be9d991
document   settings
apples
bananas    1
cantaloupe   2
dates   3
eggplant   4
figs   5
grapes
honeydew

Here's what I get for $modified_doc for example:

echo $original_doc

Name                        Value
----                           -----
document                 settings
apples
bananas                    1
cantaloupe               2
dates                        3
eggplant                  4
figs                           5
grapes
honeydew

So, in this example, the change should not happen becuase the document contains the same key-value pairs.

Here is the "if" condition for a successful document update:

if (($original_doc._id.ToString() -eq $modified_doc._id.'$oid') -and ($original_doc -ne $modified_doc)) {

I know that Get-MdbcData can be used with -Project parameter , but how do i use it? How can i get the contents of the document without the _id field for a better comparison?

This maybe just be a simple powershell question but i'm quite new to it and the Mdbc module so thanks in advance!

nightroman commented 7 months ago

It looks like a PowerShell question, yes, how to compare two dictionary like objects. I do not remember tools for doing this. We probably should do some coding like:

# document
$1 = New-MdbcData
$1._id = 1
$1.a = 2
$1.b = 3

# hashtable (assuming same keys except _id)
$2 = @{b=3; a=2}

# compare values, i.e. get different key/value pairs
$diff = $1.GetEnumerator().Where{$_.Key -ne '_id' -and $2[$_.Key] -ne $_.Value}

if ($diff) {
    "not the same: $diff"
}
else {
    'the same'
}
emnp88 commented 7 months ago

Ah thank you but I think I found my answer:

To exclude the _id when using Get-MdbcData...

$original_doc = Get-MdbcData -Project @{_id=0}

And that will give exactly this:

echo $original_doc

Name                        Value
----                           -----
document                 settings
apples
bananas                    1
cantaloupe               2
dates                        3
eggplant                  4
figs                           5
grapes
honeydew

Thank you for the quick response though!

nightroman commented 7 months ago

Great!

NB I thought you knew this but did not want to engage.. All right, you figured it all out.