2016-08-29 49 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

回答