Mongodb Search () Powershell Query

advertisements

For the following Json file, I have a mongodb query to select "Car" part of document only:

db.collection.find({_item:"Home"}, {"Car":1, "_id":0})

How do I write this query in Powershell?

{
   "_id" : 1,
   "_item": "Home",
   "Car": [
    {"name": "BMW", "model": "series 3"},
    {"name": "Audi", "model": "A4"}
   ],
   "Fruit": [
    {"name": "Banana", "color": "Yellow"},
    {"name": "Apple", "color": "Red"}
   ]
 }


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

$query = MongoDB.Driver.Builders.Query.EQ("_item","HOME")
$results = $collection.Find($query).SetFields(Fields.Exclude("_id").Include("Car"))