Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

like operator mangodb

let like = req.body.like;
// if request is a like
if (like) {
    Card.update({
        '_id': mongoose.Types.ObjectId(card_id),
        'likedBy': { '$ne': mongoose.Types.ObjectId(user_id) }
    }, {
        '$inc': { 'likes': 1 },
        '$push': { 'likedBy': mongoose.Types.ObjectId(user_id) }
    }, function(err) {
        if (err) {
          console.log(err);
           return res.json({
            'state': false,
            'msg': err
           })
        }

        return res.json({
            'state': true,
            'msg': 'Liked',
        })
    })
} else if (!like) { // if request is dislike
    Card.update({
        '_id': mongoose.Types.ObjectId(card_id),
        'likedBy': mongoose.Types.ObjectId(user_id) 
    }, {
        '$inc': { 'likes': -1 },
        '$pull': { 'likedBy': mongoose.Types.ObjectId(user_id) }
    }, function(err,) {
        if (err) {
           console.log(err);
           return res.json({
            'state': false,
            'msg': err
           })
        }

        return res.json({
            'state': true,
            'msg': 'Disliked',
        })
    })
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #operator #mangodb
ADD COMMENT
Topic
Name
3+8 =