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

139 Views Asked by At

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

There are 1 best solutions below

0
MrVoodoo On

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: Screenshot of PS 7.2.13

Test arguments with PS 7.3.6: Screenshot of PS 7.3.6

Notice how quotes are handled differently.