首页 > 解决方案 > 你能告诉我,如何在powershell中为日期开发参数化查询?

问题描述

Start-Process -FilePath "C:\Program Files\MongoDB\Tools\100\bin\mongoexport.exe" -ArgumentList ' --db=rndDB --collection=Messages --type=csv --fields Name,Message,Address ,Start_date,End_date -q "{"Start_date":{"$gte":{"$date":"2021-02-02T22:30:00.000Z"},"$lt":{"$date":" 2021-02-03T22:30:00.000Z"} } }" --out C:\Collection\Date_r2.csv'

标签: powershellmongoexport

解决方案


要扩展变量,必须将它们放在双引号中。我假设您需要其他双引号,因此您可以使用反引号将它们转义`

Start-Process -FilePath "C:\Program Files\MongoDB\Tools\100\bin\mongoexport.exe" -ArgumentList (
    ' --db=rndDB' +
    ' --collection=Messages' +
    ' --type=csv' +
    ' --fields Name,Message,Address,Start_date,End_date' +
    "-q `"{`"Start_date`":{`"$gte`":{`"$date`":`"2021-02-02T22:30:00.000Z`"},`"$lt`":{`"$date`":`"2021-02-03T22:30:00.000Z`"} } }" +
    '--out C:\Collection\Date_r2.csv'
)

或者组合单引号字符串:

'-q "{"Start_date":{"'+$gte+'":{"'+$date+'":"2021-02-02T22:30:00.000Z"},"'+$lt+'":{"'+$date+'":"2021-02-03T22:30:00.000Z"} } }"' +

推荐阅读