I've a document that looks like
{
_id: "123xyz",
profile: {
emails: [
{
"address" : "foo@gmail.com",
"primary" : true
},
{
"address" : "bar@gmail.com",
"primary" : false
}
]
}
}
When a user set an email address as primary, and if he already has other emails, I want to set those other emails as non primary ie I'd like to give all the emails different from the new primary one the flag primary: false
. According to some other SO answers like this one, this should work:
db.users.update(
{ _id: userId, 'profile.emails.address': { $ne: newEmailAddress } },
{ $set: { 'profile.emails.$.primary': false } }
);
But it fails with The positional operator did not find the match needed from the query. Unexpanded update: profile.emails.$.primary
The original document currently has just one email that differs from newEmailAddress
.