D
D
dvplut2015-12-29 20:18:26
MongoDB
dvplut, 2015-12-29 20:18:26

How can you increment a value in Mongo?

Hello!
Happy New Year to you!
Pilot project on Meteor. News aggregator.
Database structure on Mongo:

{_id: channelId, 
title: channelTitle, 
pubDate: channelPubdate, 
items: [  // массив объектов новостей
{title: newsTitle, desc: newsDescription, link: newsLink, pubDate: Date, clicks: 0}, 
{}, 
{}, 
...] }

Event handler:
Template.newsItems.events({
    "click .news-body-custom": function(e, t) {
        var iframe = document.getElementsByName("news-iframe")[0];
        iframe.src = this.link;

        var target = e.target;

        while (target.className !== "news-body-custom") {
            target = target.parentNode;
        }

        var clicks = target.getElementsByClassName("clicks")[0];
        clicks.innerHTML = +clicks.innerHTML + 1;
        debugger;

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

When there is a "click" on the news, you need to save it to the database (we count the number of clicks). For this, the "clicks" field has been created.
What query can do this? Is it possible or necessary to change the structure of the database at all?
The news is uniquely identified by the link, so it can essentially be considered the id of this news.
The one I posted above doesn't work. Roughly speaking, the request should work like this: find a news channel with channelId such and such in which there is a news object (which was clicked) with a link such and such and increment the clicks field of this news by 1.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem, 2015-12-31
@mrRontgen

You are most likely passing a string representation of the ObjectId to channelId. Try converting the channelId string to ObjectId, i.e. should get the following
The rest of the code should just work.
See stackoverflow.com/questions/19236685/how-do-i-use-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question