json - 来自 Powershell 的 Mongodb Find() 查询

标签 json mongodb powershell

对于以下 Json 文件,我有一个 mongodb 查询来仅选择文档的“Car”部分:

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

如何在 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"))

关于json - 来自 Powershell 的 Mongodb Find() 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27752692/

相关文章:

json - 简化 2 个类似的 json 辅助函数

PHP : To serialize or not

c# - MongoDB + .NET Core 1.0?

arrays - 如何从 MongoDB 中的所有子数组中删除元素

powershell - PowerShell 中的递归通配符

windows - 根据包含 2 个下划线的文件名创建一个文件夹,然后将该文件放入该文件夹中

javascript - 最初使用 Angular JS 将事件类添加到第一个 JSON 数据对象的导航链接

mongodb - mongodb中如何将字符串转换为日期?

azure - 设置-AzContext : Please provide a valid tenant or a valid subscription

android - 使用 robospice google http 客户端在 Post 中发送 JSON