Mongo - Update elements in nested array
<p>I've a document that looks like</p>

<pre><code>{
  _id: "123xyz",
  profile: {
    emails: [
      {
        "address" : "foo@gmail.com",
        "primary" : true
      },
      {
        "address" : "bar@gmail.com",
        "primary" : false
      }
    ]
  }
}
</code></pre>

<p>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 <code>primary: false</code>. According to some other SO answers like <a href="https://stackoverflow.com/questions/33371801/meteor-mongo-nested-arrays-update">this one</a>, this should work:</p>

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

<p>But it fails with <code>The positional operator did not find the match needed from the query. Unexpanded update: profile.emails.$.primary</code></p>

<p>The original document currently has just one email that differs from <code>newEmailAddress</code>.</p>