mongodump on Powershell with –query option : "error parsing query as extended json", "error parsing command line"

huangapple go评论56阅读模式
英文:

mongodump on Powershell with --query option : "error parsing query as extended json", "error parsing command line"

问题

尝试部分导出一个集合使用 mongodump

mongodump --uri "mongodb+srv://l:p@host/db" -o folder --collection collection --query '{ "meta.field": {"$gte": {"$date": "2023-01-01T00:00:00Z" }}}'

在搜索了stackoverflow和mongo论坛后,我尝试使用\来转义引号,使用三重引号括起来,使用双引号括起来,并在查询内部使用单引号。每次都会出现标题中提到的错误之一。

英文:

Trying to partially dump a collection with mongodump:

mongodump --uri "mongodb+srv://l:p@host/db" -o folder --collection collection --query '{ "meta.field": {"$gte": {"$date": "2023-01-01T00:00:00Z" }}}'

After searching on stackoverflow and mongo forums, I tried using \ to escape the quotes, enclosing with triple quotes, enclosing with double quotes and using single quotes inside the query. Every time I get one of the errors mentioned in title.

答案1

得分: 2

Answer for powershell version < 7.3:

经过一些尝试和错误,我发现这个方法有效(也许在某个论坛的某个地方已经提到过,但我找不到):

mongodump --uri "mongodb+srv://l:p@host/db" -o folder --collection collection --query "{ `&quot;meta.field`&quot;: {`&quot;`$gte`&quot;: {`&quot;`$date`&quot;: `&quot;2023-01-01T00:00:00Z`&quot; }}}&quot;"

显然,反引号用作转义字符,而不是反斜杠。

Edit

Wernfried的评论让我深入了解了一些更多信息。
从Powershell 7.3开始,参数传递方式不同:https://learn.microsoft.com/en-us/powershell/scripting/learn/experimental-features?view=powershell-7.3#psnativecommandargumentpassing

所以我的初始命令(在问题中)现在在最新的Powershell版本中可用。

使用PS 7.2.13测试参数:
mongodump on Powershell with –query option : "error parsing query as extended json", "error parsing command line"

使用PS 7.3.6测试参数:
mongodump on Powershell with –query option : "error parsing query as extended json", "error parsing command line"

请注意引号的处理方式不同。

英文:

Answer for powershell version < 7.3:

After some trial and error, I found this works (maybe this was mentioned somewhere on some forum but I couldn't find it):

mongodump --uri &quot;mongodb+srv://l:p@host/db&quot; -o folder --collection collection --query &quot;{ `&quot;meta.field`&quot;: {`&quot;`$gte`&quot;: {`&quot;`$date`&quot;: `&quot;2023-01-01T00:00:00Z`&quot; }}}&quot;

Apparently, backticks are used as escape and not backslashes.

Edit

Wernfried's comment made be dig a bit more.
Since Powershell 7.3, arguments are passed differently : https://learn.microsoft.com/en-us/powershell/scripting/learn/experimental-features?view=powershell-7.3#psnativecommandargumentpassing

So my initial command (in the question) now works in latest Powershell version.

Test arguments with PS 7.2.13:
mongodump on Powershell with –query option : "error parsing query as extended json", "error parsing command line"

Test arguments with PS 7.3.6:
mongodump on Powershell with –query option : "error parsing query as extended json", "error parsing command line"

Notice how quotes are handled differently.

huangapple
  • 本文由 发表于 2023年7月17日 17:51:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76703261.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定