我有一个看起来像的文档
{
_id: "123xyz",
profile: {
emails: [
{
"address" : "[email protected] /cdn-cgi/l/email-protection",
"primary" : true
},
{
"address" : "[email protected] /cdn-cgi/l/email-protection",
"primary" : false
}
]
}
}
当用户将一个电子邮件地址设置为主要电子邮件地址时,如果他已经有其他电子邮件地址,我想将这些其他电子邮件设置为非主要电子邮件地址,即我想为所有与新主要电子邮件不同的电子邮件添加标志primary: false
。根据其他一些答案,例如this one https://stackoverflow.com/questions/33371801/meteor-mongo-nested-arrays-update,这应该有效:
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
.
我在这里找到了你问题的答案:更新数组中存在的数组项 https://stackoverflow.com/questions/24166615/update-an-item-in-an-array-that-is-in-an-array
在这种情况下使用该解决方案(使用您的结构):
db.mail.update({"profile.emails": {$elemMatch: {"address":
{$ne: "[email protected] /cdn-cgi/l/email-protection" }}}}, {$set: {"profile.emails.$.primary": "false"}})
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)