2016-08-29 117 views
2

我有,看起来像蒙戈 - 在嵌套数组元素更新

{ 
    _id: "123xyz", 
    profile: { 
    emails: [ 
     { 
     "address" : "[email protected]", 
     "primary" : true 
     }, 
     { 
     "address" : "[email protected]", 
     "primary" : false 
     } 
    ] 
    } 
} 

当用户设置一个电子邮件地址作为主,和文档时,如果他已经有其他的电子邮件,我想设置与其他电子邮件作为非主要的即我想给所有的电子邮件不同于新的主要的一个国旗0​​。根据像this one一些其他的SO答案,这应该工作:

db.users.update(
    { _id: userId, 'profile.emails.address': { $ne: newEmailAddress } }, 
    { $set: { 'profile.emails.$.primary': false } } 
); 

但它失败The positional operator did not find the match needed from the query. Unexpanded update: profile.emails.$.primary

原稿目前只有一个电子邮件从newEmailAddress不同。

+0

请更多你想要什么细说吗? –

+0

嗯,我希望不同于'newEmailAddress'的电子邮件获得标志'primary:false',并且查询在没有失败的情况下运行 – Guig

回答