How to increment a field in mongodb?

As the error indicates, on the client you can only perform an update with a simple _id selector. I’d recommend using a method with a slight modification to your code:

Meteor.methods({
  incClicks: function(id, news) {
    check(id, String);
    check(news, Match.ObjectIncluding({link: String}));

    News.update(
      {_id: id, 'items.link': news.link},
      {$inc: {'items.$.clicks': 1}}
    );
  }
});

Here we are using the $ operator to update the specific embedded document. See the docs for more details.

Leave a Comment