0

This is the database

var UserSchema = new mongoose.Schema({
    profilePicture: {type: String, default: '../static/img/placeholder.jpg'},
    country: String,
    email: {
        type: String,
        unique: true,
        required: true,
        trim: true
    },
    name: {
        type: String,
        required: true,
        trim: true
    },
    password: {
        type: String,
        required: true
    },
    discussions: [{
            title: String,
            details: String,
            createdAt: {type: Date, default: Date.now},
            imgPath: {type: String, default: '../static/img/placeholder.png'},
            votesUp: {type: Number, default: 0},
            votesDown: {type: Number, default: 0},
            viewCount: {type: Number, default: 0},
            discussionsId: String,
            discussionReplay: [{
                    comment: String,
                    commentImg: String,
                    commentDate: {type: Date, default: Date.now},
                    commentOwner_id: String,
                    commentOwnerImg: String,
                    commentOwnerName: String,
                    commentVotesUp: {type: Number, default: 0},
                    commentVotesDown: {type: Number, default: 0}
            }]
    }]
});

I want to increment commentVotesUp but i have so many comments in discussionReplay array. This code works but this is static. i can only access to the 0 indexed comment object. How can i make that the zero dynamic. discussions.$.discussionReplay.0.commentVotesUp Is there anyway of passing a variable?

        User.update({_id: req.body.user_id, discussions: {
          $elemMatch: { discussionsId: req.body.discussion_id} }},
                    {$inc: { "discussions.$.discussionReplay.0.commentVotesUp" : 1}},
                        function(err,item){
                            res.send('working')
        });
  • Welcome to Stack Overflow. Please read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) which includes such things as "good" and "bad" titles for a question. "Cannot do X" is not a good title. Instead, summarize the "intent" of the question, rather than the "result". It also helps you in searching for existing answers before you post. Such as has been linked here. – Neil Lunn Oct 31 '17 at 10:26
  • Neil My question wasn't duplicate. i wasn't just trying to access inside of an array, i wanted to figure out a way of doing it dynamicly –  Oct 31 '17 at 11:34
  • Dude. That's exactly what is answered in the linked question. Nested arrays cannot be presently updated "dynamically". So you are presented with how you should really approach redesigning your nested arrays, or wait until it's actually supported. Or of course have a horrible process that's essentially not atomic. Which all is explained if you simply take the time to read. You have nested arrays you are trying to update, and that is your question. Learn from the answers. – Neil Lunn Oct 31 '17 at 11:39

0 Answers0