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:
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:
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:
Invoke-Expression '& "C:\MongoDB\bin\mongo.exe" localhost:27017/mydb --eval "db.mydata.update({}, {`$set : {v : 2}})" --quiet'
Try using single quotes instead of double quotes around the eval statement.
FYI this isn't just specific to PowerShell, it's applicable to running mongo on any platform (Windows, OSX, etc)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With