Meteor: Uncaught Error: Not permitted. Untrusted code may only update documents by ID when update array element using $

Error: Not permitted. Untrusted code may only update documents by ID when update sub array using $ in Meteor

Cause:

Suppose we have this doc:

{

_id: 1;

tags: [

{tag: “tag1″, count: 0},

{tag: “tag2″, count: 0},

]

}

And we want to update tag1 count by inc with 1 with a client-side script:

db.tags.update({_id:1, “tags.tag”: “tag1″}, {$inc: {“tags.$.count”: 1}})

Even the above update only update a single document, Meteor will not allow us to do so because it expects only _id in the update condition.

Solution: Update the whole tags with a modified value for tag1 count:

db.tags.update({_id:1}, {$set: {“tags”: [{tag: “tag1″, count: 1},{tag: “tag2″, count: 0},]}});

Meteor: Uncaught Error: Not permitted. Untrusted code may only update documents by ID when update array element using $