How to increment a field in mongodb?

萝らか妹 提交于 2019-12-28 04:27:05

问题


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.



来源:https://stackoverflow.com/questions/27707365/how-to-increment-a-field-in-mongodb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!