Jake 1986 Jake 1986 - 2 months ago 8
jQuery Question

How to $inc MongoDB Document Field with AJAX?

I am trying to $inc a MongoDB field each time a "Like" button is clicked. Because there is no data that I need to pass to the server (but maybe there is?), I am not sure what value to put for the

data
key in my
.ajax
call.

The post route below shows what I am trying to do when the "Like" button is clicked:

app.post('/like', function(req,res){
var query = req.session.mostRecentQuiz;
db.collection('mycollection').update(
query, { $inc: {"likes": 1 } }, function (err, result) {
if (err) throw err;
console.log(result);
} );
});


On page JS:

$("#like").click(function(){
$.ajax({
url: "/like",
method: "POST",
data: , //what would go here?
//I just need to $inc each time "Like is clicked.
success: function(data){
if(data.success){
console.log("success!")
}else{
console.log("failure.")
}
}
})
});


Thanks in advance for any answers.

Answer

true for like, false for unlike

Comments