Mongodb datetime不返回结果的新MdbcQuery
我正在尝试从我的收藏中获取早于X日期的文档列表。但是,我的查询不适用于datetime 我有一个mongo atlas集群,包含多个数据库和集合。我想查找特定集合并删除该集合下早于X日期和时间的文档。我可以获取记录列表并对powershell对象应用筛选器,但在检索数据时使用查询时,不会返回任何结果Mongodb datetime不返回结果的新MdbcQuery,mongodb,datetime,mongodb-query,Mongodb,Datetime,Mongodb Query,我正在尝试从我的收藏中获取早于X日期的文档列表。但是,我的查询不适用于datetime 我有一个mongo atlas集群,包含多个数据库和集合。我想查找特定集合并删除该集合下早于X日期和时间的文档。我可以获取记录列表并对powershell对象应用筛选器,但在检索数据时使用查询时,不会返回任何结果 $connstring = "mongodb+srv://User:pwd@clus56tg-a0ov.azure.mongodb.net" Connect-mdbc -ConnectionStr
$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
我已经通过了测试(注意到有些测试使用datetime,解释查询如下所示)。
测试{New MdbcQuery Name-In$date}{“Name”:{“$In”:[ISODate(“2011-11-11T00:00:00Z”)]}
但在我的例子中,日期并不是围绕ISODate的。我缺少的是在查询中转换数据类型 $date=“2019年9月16日下午0:00:00” $query=新的MdbcQuery DateTime-LTE([DateTime]$date) 我需要对guid和其他数据类型执行相同的操作 感谢@nightroman的帮助