我收集foo
了以下文件:
{site_id: 'xxx', title: {ru: 'a', en: 'b'}, content: {ru: 'a', en: 'b'}}
{site_id: 'xxx', title: {ru: 'c', de: 'd'}, content: {ru: 'c', de: 'd'}}
我需要更新可以存在或不存在的多个字段:
db.foo.update(
{ site_id: 'xxx'},
{ $set: {'title.de': '', 'content.de': ''}},
{multi: true}
)
但是我需要类似的东西$set
,如果存在,它将不会覆盖值。
您可以在更新语句中添加查询:
db.foo.update({'title.de': {$exists : false}}, {$set: {'title.de': ''}})
对于您提出的修改过的问题,我的解决方案看起来像这样-对您有用吗?(如果没有,为什么?)
db.foo.update({site_id: 'xxx', 'title.de': {$exists : false}}, {$set: {'title.de': ''}, {multi: true})
db.foo.update({site_id: 'xxx', 'content.de': {$exists : false}}, {$set: {'content.de': ''}}, {multi: true})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句