【发布时间】:2016-03-07 20:53:24
【问题描述】:
我需要使用 Powershell 从 mongoDB 检索数据。 假设我有 db.orders 集合,只需要检索上周创建的订单,并且只检索特定列,例如 _id、status、createdAt 字段。
订单集合架构
{
"_id": ObjectId("56cf9bab78e9fd46ec557d69"),
"status" : "ordered",
...
"total": 343,
"createdAt": ISODate("2016-01-15T17:29:09.342Z")
}
我可以像这样在 mongo shell 中查询它
db.orders.find({
"createdAt" : {
$lt: new Date(),
$gte: new Date(new Date().setDate(new Date().getDate()-7))
}
}, {_id: 1, status: 1, createdAt: 1 })
但我需要在 Powershell 中执行此操作,这是我的 Powershell 脚本,其中包含简单查询,可准确提取 createdAt 日期.. 不是日期范围
$mongoDbDriverPath = "C:\mongodb\bin"
$dbName = "Orders"
$collectionName = "orders"
Add-Type -Path "$($mongoDbDriverPath)\MongoDB.Bson.dll"
Add-Type -Path "$($mongoDbDriverPath)\MongoDB.Driver.dll"
$db =[MongoDB.Driver.MongoDatabase]::Create("mongodb://localhost:27017/$($dbName)")
$collection = $db[$collectionName]
$query = [MongoDB.Driver.Builders.Query]::EQ("createdAt","2016-01-15T17:29:09.342Z")
$results = $collection.find($query)
在 MongoDB .NET Driver api 中,我无法进行复杂的查询,或者至少不知道如何进行。 我可以根据一个特定的列查询,但不能做复杂的,不能限制某些字段的输出。
如果有人知道怎么做,请告知。 注意:它不是一个.Net项目,它只是使用mongoDB .net驱动,而是在Powershell中执行的。
【问题讨论】:
-
您使用什么版本的驱动程序?
-
当前使用1.7.0.4714
-
好的,我在 2.2.3 上运行它时遇到问题 :-) - 明天看看,因为这是一个很好的破解
标签: mongodb powershell mongodb-query mongo-shell mongodb.driver