Error calling MongoDB update using --eval switch shell from Powershell

I am trying to call a MongoDB javascript fragment using the mongo.exe --eval command line switch. This works fine when run from a Windows command prompt, but I want to call it from a Powershell script as follows:

Invoke-Expression "& `"C:\MongoDB\bin\mongo.exe`" localhost:27017/mydb --eval `"db.mydata.update({}, {`$set : {v : 1}})`" --quiet" 

There is only one document in the mydata collection, and I want to set its field v to 1 . But the above expression returns SyntaxError: invalid property id (shell eval):1 when run from a Powershell script and does not update the document.

What is even more confusing is that the work works as expected:

 Invoke-Expression "& `"C:\MongoDB\bin\mongo.exe`" localhost:27017/mydb --eval `"printjson(db.mydata.findOne())`" --quiet" 

Any ideas what I can do wrong?

Update:

Decision:

 Invoke-Expression '& "C:\MongoDB\bin\mongo.exe" localhost:27017/mydb --eval "db.mydata.update({}, {`$set : {v : 2}})" --quiet' 
+4
source share
3 answers

Try using single quotes instead of double quotes around the eval statement.

+14
source

FYI is not just PowerShell specific, it is applicable to run mongo on any platform (Windows, OSX, etc.)

+1
source

@Wes Freeman saved my day!

Using MongoDB Mongo

Use single quotes (e.g. ') to enclose JavaScript, as well as additional JavaScript needed to generate this output.

0
source

Source: https://habr.com/ru/post/1392945/


All Articles