英文:
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 "{ `"meta.field`": {`"`$gte`": {`"`$date`": `"2023-01-01T00:00:00Z`" }}}""
显然,反引号用作转义字符,而不是反斜杠。
Edit
Wernfried的评论让我深入了解了一些更多信息。
从Powershell 7.3开始,参数传递方式不同:https://learn.microsoft.com/en-us/powershell/scripting/learn/experimental-features?view=powershell-7.3#psnativecommandargumentpassing
所以我的初始命令(在问题中)现在在最新的Powershell版本中可用。
请注意引号的处理方式不同。
英文:
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 "mongodb+srv://l:p@host/db" -o folder --collection collection --query "{ `"meta.field`": {`"`$gte`": {`"`$date`": `"2023-01-01T00:00:00Z`" }}}"
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:
Notice how quotes are handled differently.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论