Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用powershell按日期范围查询MongoDB时出现的问题_Mongodb_Powershell_Mongodb .net Driver - Fatal编程技术网

使用powershell按日期范围查询MongoDB时出现的问题

使用powershell按日期范围查询MongoDB时出现的问题,mongodb,powershell,mongodb-.net-driver,Mongodb,Powershell,Mongodb .net Driver,我无法使powershell中的查询适用于某个日期范围。下面是我的mongodb文档和powershell代码。我必须切换到UTC时间以匹配mongodb中的时间。我尝试了几种不同的日期格式,但在查询日期范围甚至特定日期时仍然找不到任何记录。任何帮助都将不胜感激 MongoDB: { "_id" : ObjectId("570d0955ef8ca41768d887a8"), "HostName" : "blah.foo.bar", "Maintenance" : false, "Proc

我无法使powershell中的查询适用于某个日期范围。下面是我的mongodb文档和powershell代码。我必须切换到UTC时间以匹配mongodb中的时间。我尝试了几种不同的日期格式,但在查询日期范围甚至特定日期时仍然找不到任何记录。任何帮助都将不胜感激

MongoDB:

{ 
"_id" : ObjectId("570d0955ef8ca41768d887a8"), 
"HostName" : "blah.foo.bar", 
"Maintenance" : false, 
"Process" : false, 
"ProcessDateTime" : ISODate("2016-04-12T14:39:53.000+0000")
}
Powershell:

$mongoDbDriverPath = "C:\utilities\mongoDB\"
$dbName = "blahProd"
$collectionName = "blah"
Add-Type -Path "$($mongoDbDriverPath)\MongoDB.Bson.dll"
Add-Type -Path "$($mongoDbDriverPath)\MongoDB.Driver.dll"
$db = [MongoDB.Driver.MongoDatabase]::Create("mongodb://localhost/($dbname)")
$collection = $db[$collectionName]

$rundate = Get-Date
$rundate = $rundate.AddMinutes(-30)
$rundate_utc = $rundate.ToUniversalTime()
$ISOrundate_utc = $rundate_utc.ToString("yyyy-MM-ddTHH:mm:ssZ")

$query =[MongoDB.Driver.Builders.Query]::GTE"ProcessDateTime",$ISOrundate_utc)

$results = $collection.find($query)
foreach ($result in $results) {
    write-host $result["HostName"] " - " $result["ProcessDateTime"]
}
看起来很有意思


似乎可以做到这一点。

在power shell脚本中使用c#代码可能会更容易-在power shell脚本中使用c#代码可能会更容易-您是否可以为您的答案添加更多的上下文,如OPs实现中缺少的内容以及为什么这样做会更好?您是否可以为您的答案添加更多上下文,如OPs中缺少的内容实施以及为什么这会更好?
$query2 = [MongoDB.Driver.Builders.Query]::GTE("Time", [dateTime]"2021-06-01")
$query3 = [MongoDB.Driver.Builders.Query]::LT("Time", [dateTime]"2021-06-09")