【问题标题】:New-MdbcQuery with datetime not returing the results带有日期时间的 New-Jdbc 查询不返回结果
【发布时间】:2019-09-19 16:01:49
【问题描述】:

我正在尝试从我的收藏中获取早于 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)and 注意到一些日期时间和解释的查询看起来像这样。 test { New-MdbcQuery Name -In $date } '{ "Name" : { "$in" : [ISODate("2011-11-11T00:00:00Z")] } }'

但在我的情况下,日期没有包含在 ISODate 周围。

【问题讨论】:

    标签: mongodb datetime mongodb-query


    【解决方案1】:

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

    $date = "2019 年 9 月 16 日下午 0:00:00" $query = New-MdbcQuery DateTime -LTE ([datetime]$date)。

    我需要为 guid 和其他数据类型做同样的事情。

    感谢@nightroman 的帮助。

    【讨论】:

      猜你喜欢
      • 2019-10-19
      • 1970-01-01
      • 1970-01-01
      • 2021-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多