使用Powershell中的shell --eval开关调用MongoDB更新时遇到问题 [英] Trouble invoking MongoDB update using shell --eval switch from Powershell

查看:107
本文介绍了使用Powershell中的shell --eval开关调用MongoDB更新时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用mongo.exe --eval命令行开关调用MongoDB javascript片段.从Windows命令行运行时,此方法工作正常,但我想从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"

mydata集合中只有一个文档,我想将其v字段设置为1.但是,从Powershell脚本运行时,上面的表达式返回SyntaxError: invalid property id (shell eval):1,并且不会更新文档.

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?

更新:

解决方案:

Invoke-Expression '& "C:\MongoDB\bin\mongo.exe" localhost:27017/mydb --eval "db.mydata.update({}, {`$set : {v : 2}})" --quiet'

推荐答案

尝试在eval语句周围使用单引号而不是双引号.

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

这篇关于使用Powershell中的shell --eval开关调用MongoDB更新时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆