Search code examples
mongodbpowershellmongodb-.net-drivermongodb-query

Powershell querying MongoDB


1) Here's my schema:

{
    "_id" : ObjectId("53f4db1d968166157c2d57ce"),
    "init" : "SJ",
    "name" : "Steve Jobs",
    "companies" : [ 
        {
            "_id" : ObjectId("53f4db1d968166157c2d57cf"),
            "ticker" : "AAPL",
            "compname" : "Apple"
        }, 
        {
            "_id" : ObjectId("53f4db1d968166157c2d57d0"),
            "ticker" : "MSFT",
            "compname" : "Microsoft"
        }, 
        {
            "_id" : ObjectId("53f4db1d968166157c2d57d1"),
            "ticker" : "ABC",
            "compname" : "iTunes"
        }, 
        {
            "_id" : ObjectId("53f4db1d968166157c2d57d2"),
            "ticker" : "DEF",
            "compname" : "iPad Mini"
        }
    ]
}

I'm trying to get a list of compnames, using Powershell & MongoDB. Here's what I have so far:

$databaseName = "CompanyInfo"
$collectionName = "comps"
$client = New-Object -TypeName MongoDB.Driver.MongoClient -ArgumentList "mongodb://localhost:27017"
$server = $client.GetServer()
$database = $server.GetDatabase($databaseName)
$collection = $database.GetCollection($collectionName)

$query['init'] = "SJ"
$results = $collection.FindOne($query)
foreach ($result in $results) {
    write-host $result["companies.ticker"] /// Doesn't show me any records
}

This doesn't show me any records. How can I display companies.ticker info where init = "SJ"?

2) Btw, I get the following error after

$query['init'] = "SJ"

error

Cannot index into a null array.
At line:9 char:1
+ $query['init'] = "SJ"
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Any ideas as to why? I only have the MongoDB's standard index, which is on "_id", nothing else. My powershell script still works but I'm curious as to why I get that error.

[UPDATE Part 2] Thanks to @arco444, I no longer get error in part 2. Here's my revised code:

$query = @{'init' = "SJ"}
$collection.FindOne([MongoDB.Driver.QueryDocument]$query)

But I actually need help with part 1 - which is to display only the company tickers for a particular init. Any ideas on that one?

[ANSWER Part 1] Thanks again to @arco444 for directing me to the right path. After some tinkering around, I figured out what I missed. Here's my updated code:

$databaseName = "CompanyInfo"
$collectionName = "comps"
$client = New-Object -TypeName MongoDB.Driver.MongoClient -ArgumentList "mongodb://localhost:27017"
$server = $client.GetServer()
$database = $server.GetDatabase($databaseName)
$collection = $database.GetCollection($collectionName)

$query = new-object MongoDB.Driver.QueryDocument("init","SJ") /// Updated
$results = $collection.FindOne($query)
foreach ($result in $results["companies"]) { /// Updated
    write-host $result["ticker"] /// Updated
}

Solution

  • Here's my updated code:

    $databaseName = "CompanyInfo"
    $collectionName = "comps"
    $client = New-Object -TypeName MongoDB.Driver.MongoClient -ArgumentList "mongodb://localhost:27017"
    $server = $client.GetServer()
    $database = $server.GetDatabase($databaseName)
    $collection = $database.GetCollection($collectionName)
    
    $query = new-object MongoDB.Driver.QueryDocument("init","SJ") /// Updated
    $results = $collection.FindOne($query)
    foreach ($result in $results["companies"]) { /// Updated
        write-host $result["ticker"] /// Updated
    }