Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble invoking MongoDB update using shell --eval switch from Powershell

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'
like image 829
Joe Waller Avatar asked Jan 25 '12 13:01

Joe Waller


2 Answers

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

like image 50
Eve Freeman Avatar answered Sep 23 '22 15:09

Eve Freeman


FYI this isn't just specific to PowerShell, it's applicable to running mongo on any platform (Windows, OSX, etc)

like image 40
DarylChymko Avatar answered Sep 23 '22 15:09

DarylChymko