如何从命令行更新mongodb字符串字段

How to update a mongodb string field from command line?

本文关键字:mongodb 字符串 字段 更新 命令行      更新时间:2023-09-26

我正在尝试从命令行(而不是从mongo shell)更新字段

mongo mydb --eval "db.users.update({}, { $set : {  email : "email@email.com" } })"

结果输入

Fri Oct 24 12:23:46.102 JavaScript execution failed: SyntaxError: Unexpected token :

再次尝试

mongo mydb --eval "db.users.update({}, { $set : {  email : '"email@email.com'" } })"

相同的结果

Fri Oct 24 12:24:05.559 JavaScript execution failed: SyntaxError: Unexpected token :

有什么帮助吗?

这基本上只是引用。shell在内部通常更宽容,但在其他方面确实期望有效的JSON:

mongo mydb --eval "db.users.update({}, { '$set': {  'email' : 'email@email.com' } })"
MongoDB shell version: 2.6.5
connecting to: mydb
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

实际上,为了更好地使用其他修饰符(如multi),请反转使用的引号类型:

mongo mydb --eval 'db.users.update({}, { "$set": {  "email": "email@email.com" } },{ "multi": true })