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')
});