nightroman / Mdbc

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

Unable to project the third level data with Get-MdbcData #62

Closed saptaji closed 3 years ago

saptaji commented 3 years ago

I specified the project field as follow:

{   "_id": 0,"Agent.Os": 1,"Agent.AgentDevice.AgentId": 1,"Agent.AgentDevice.DeviceId": 1}

And this is the output:

Agent : @{AgentDevice=; Os=CentOS Linux release 7.8.2003 (Core)}

This is the json format in the database:

/* 1 */
{
    "_id" : "5df259d57ca49211d4c00119",
    "Agent" : {
        "AgentDevice" : {
            "AgentId" : "007",
            "DeviceId" : "007"
        },
        "Os" : "CentOS Linux release 7.8.2003 (Core)",
    }  
}          

How I can get that 'Agent.AgentDevice.AgentId' data?

nightroman commented 3 years ago

Not sure what you mean. Here are some bits you may get:

Import-Module Mdbc
Connect-Mdbc -NewCollection

@{
    _id = 0
    Agent = @{
        Os = 1
        AgentDevice = @{
            AgentId = 1
            DeviceId = 1
        }
    }
} |
Add-MdbcData

$r = Get-MdbcData

'ToString()'
$r.ToString()

'Print()'
$r.Print()

# is this it?
'Agent.AgentDevice.AgentId'
$r.Agent.AgentDevice.AgentId

Output:

ToString()
{ "_id" : 0, "Agent" : { "Os" : 1, "AgentDevice" : { "DeviceId" : 1, "AgentId" : 1 } } }
Print()
{
  "_id" : 0,
  "Agent" : {
    "Os" : 1,
    "AgentDevice" : {
      "DeviceId" : 1,
      "AgentId" : 1
    }
  }
}
Agent.AgentDevice.AgentId
1
saptaji commented 3 years ago

This is the command that I am using:

$deviceFielddProjection = '{   "_id": 0,"Agent.Os": 1,"Agent.AgentDevice.AgentId": 1,"Agent.AgentDevice.DeviceId": 1}'

Get-MdbcData -As PS -Project $deviceFielddProjection

If I didn't specify '-Project' parameter, it will return all of the data successfully. The data that I posted in this ticket is only a subset of it. I want to reduce the amount of data that is being transferred that's why I am using the '-Project' parameter.

Update: When I removed '-As PS', the Projection works fine.

$deviceFielddProjection = '{   "_id": 0,"Agent.Os": 1,"Agent.AgentDevice.AgentId": 1,"Agent.AgentDevice.DeviceId": 1}'

Get-MdbcData -Project $deviceFielddProjection

I am still interested in specifying '-As PS' parameter. If you can provide an example, I appreciate it.

nightroman commented 3 years ago

This is explained better. I think it works, it's just PS formatting of PS objects with nested data confuses you.

Import-Module Mdbc
Connect-Mdbc -NewCollection

@{
    _id = 0
    Agent = @{
        Os = 1
        AgentDevice = @{
            AgentId = 2
            DeviceId = 3
        }
    }
} |
Add-MdbcData

$deviceFielddProjection = '{"_id": 0, "Agent.Os": 1, "Agent.AgentDevice.AgentId": 1, "Agent.AgentDevice.DeviceId": 1}'

$r = Get-MdbcData -As ps -Project $deviceFielddProjection
$r | ConvertTo-Json -Depth 99

Output:

{
    "Agent":  {
                  "Os":  1,
                  "AgentDevice":  {
                                      "DeviceId":  3,
                                      "AgentId":  2
                                  }
              }
}

All is projected as you asked. Or not?

saptaji commented 3 years ago

It works when using a json format. All fields that were specified in the Project parameter, returned properly.

Thank you for your support. I appreciate it.