Question:
I am trying to invoke a MongoDB javascript fragment using the mongo.exe –eval command line switch. This works fine when run from the Windows command line, but I want to invoke it from a Powershell script like so:
1 2 |
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 v
field 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 makes this even more confusing is that the follow works as expected:
1 2 |
Invoke-Expression "& `"C:\MongoDB\bin\mongo.exe`" localhost:27017/mydb --eval `"printjson(db.mydata.findOne())`" --quiet" |
Any ideas what I might be doing wrong?
Update:
The solution:
1 2 |
Invoke-Expression '& "C:\MongoDB\bin\mongo.exe" localhost:27017/mydb --eval "db.mydata.update({}, {`$set : {v : 2}})" --quiet' |
Answer:
Try using single quotes instead of double quotes around the eval statement.