title: Powershell querying MongoDB
body: <p>1)  Here's my schema:</p>
  
  <pre><code>{
      "_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"
          }
      ]
  }
  </code></pre>
  
  <p>I'm trying to get a list of compnames, using Powershell &amp; MongoDB.  Here's what I have so far:</p>
  
  <pre><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['init'] = "SJ"
  $results = $collection.FindOne($query)
  foreach ($result in $results) {
      write-host $result["companies.ticker"] /// Doesn't show me any records
  }
  </code></pre>
  
  <p>This doesn't show me any records.  How can I display companies.ticker info where init = "SJ"?</p>
  
  <p>2)  Btw, I get the following error after</p>
  
  <pre><code>$query['init'] = "SJ"
  </code></pre>
  
  <p>error</p>
  
  <pre><code>Cannot index into a null array.
  At line:9 char:1
  + $query['init'] = "SJ"
  + ~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
      + FullyQualifiedErrorId : NullArray
  </code></pre>
  
  <p>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.</p>
  
  <p><strong>[UPDATE Part 2]</strong> Thanks to @arco444, I no longer get error in part 2.  Here's my revised code:</p>
  
  <pre><code>$query = @{'init' = "SJ"}
  $collection.FindOne([MongoDB.Driver.QueryDocument]$query)
  </code></pre>
  
  <p>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?</p>
  
  <p><strong>[ANSWER Part 1]</strong> 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:</p>
  
  <pre><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
  }
  </code></pre>