具有日期时间的新 Mdbc 查询不会重新调整结果



我正在尝试从我的收藏中获取早于 X 日期的文档列表。但是我的查询不适用于日期时间。

我有一个包含多个数据库和集合的 mongo atlas 集群。我想查找特定集合并删除该集合下早于 X 日期和时间的文档。我可以获取记录列表并在 powershell 对象上应用过滤器,但是当我在检索数据时使用查询时,没有返回任何结果。

$connstring = "mongodb+srv://User:pwd@clus56tg-a0ov.azure.mongodb.net"
Connect-mdbc -ConnectionString $connstring 
$global:Server = $Server
Write-Host "Server `$Server $($Server.Settings.Server)"
$allDatabases = ($Server.GetDatabaseNames()).Where{($_ -notmatch 'local') -and ($_ -notmatch 'admin') -and ($_ -notmatch 'config')}
$date = "9/16/2019 0:00:00 PM"
$query = New-MdbcQuery DateTime -LTE $date
#printing the query looks like this
#{ "DateTime" : { "$lte" : "9/16/2019 0:00:00 PM" } }
$settings = New-Object MongoDB.Driver.MongoCollectionSettings
$settings.GuidRepresentation = 2
foreach($dbname in $allDatabases)
{
$database = $Server.GetDatabase($dbname)
$collections = $database.GetCollection('consumerstate', $settings)
$collections.Count() 
foreach($coll in $collections)
{
Write-Output("Database Name: $coll.Database.Name, Collection Name: $coll.Name")
###This code works###
$objs = Get-MdbcData -As PS -Collection $coll
$objs.Where{$_.DateTime -le '9/16/2019 0:00:00 PM'}
#or this works
Get-MdbcData(New-MdbcQuery _id -EQ c33af1-acef-4g6a-ai05-aase56d5)
########
###This does not return any results###
Get-MdbcData -As PS -Collection $coll -Query $query
#or this
Get-MdbcData -Collection $coll -Query $query         
return
}
}

记录输出如下所示。

Sendfeedlooppacket         {LatestRequestStatusTypeId, LatestRequestStatusDate, DisplayId, CId...}                                                                                                                                              
_id                        9f8a7-53vva-4xdr-b45c-a34hsd22e282                                                                                                                                                                                              
TenantId                   12sf3484c-e349-43a9-9951-80345sf268452                                                                                                                                                                                              
Consumer                   RequestService.MessageConsumers.RequestResponseConsumer                                                                                                                                            
DateTime                   9/19/2019 5:39:04 PM  

我已经浏览了测试(https://github.com/nightroman/Mdbc/blob/master/Tests/New-MdbcQuery.test.ps1(,并注意到一些带有日期时间的测试,解释的查询如下所示。 test { New-MdbcQuery Name -In $date } '{ "Name" : { "$in" : [ISODate("2011-11-11T00:00:00Z"(] } }'

但就我而言,日期没有环绕在 ISODate 中。

我缺少的是转换查询中的数据类型。

$date = "9/16/2019 0:00:00 PM" $query = New-MdbcQuery DateTime -LTE ([datetime]$date(。

我需要对 guid 和其他数据类型执行相同的操作。

感谢@nightroman的帮助。

最新更新