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'
source share