How to increment a field in mongodb?

2019-01-20 01:26发布

问题:

I have the following Mongo DB document structure:

{
  _id: channelId, 
  title: channelTitle,
  pubDate: channelPubdate, 
  items: 
  [
    {
      title: newsTitle,
      desc: newsDescription, 
      link: newsLink, 
      pubDate: Date, 
      clicks: 0
    },
    {/*One more*/},
    {/*...*/}
  ] 
}

I have troubles incrementing the "clicks" field in the Collection (updating the field of a document embedded in an array).

I tried this in an event handler (client):

News.update({ _id : Session.get("channelId"), "items.link" : this.link },
  { $inc: { "items.clicks": 1 } }
);

But it gives an error: Uncaught Error: Not permitted. Untrusted code may only update documents by ID. [403]

I then tried via a server method:

Meteor.methods({
    incClicks: function(id, news) 
    {
      News.update({ _id : id, "items.link" : news.link }, 
        { $inc : { "items.clicks": 1 } }
      );
    }
});

Yet, another exception: Exception while invoking method 'incClicks' MongoError: can't append to array using string field name: clicks

What would be a correct Mongo request for this action?

回答1:

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.