6

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'

3 Answers 3

20

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

Sign up to request clarification or add additional context in comments.

1 Comment

I had the same issue and this fixed it.
1

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

Comments

0

@Wes Freeman saved my day!

MongoDB Reference mongo program use

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.