0

I want to update the likes' value with 1.

My MongoDB structure is as follows .

   {"_id":{"$oid":"5f980a66c4b0d52950bdf907"},
    "course_id":"5002",
    "lecture_id":"451",
    "__v":0,
    "created_at":{"$date":"2020-10-27T11:54:14.842Z"},
    "message":[
    {
    "_id":{"$oid":"5f980a84c4b0d52950bdf90a"},
    "from":{
    "userId":"68819","name":"Developer IIRS",
    "avatar":"https://api.adorable.io/avatars/285/811753.png"
    },
    "content":"ok",
    "likes":0,
    "parent_id":null,
    "created_at":{"$date":"2020-10-27T11:54:44.388Z"}
    }
    ]
    }

Here is my code to update value of likes

    router.post('/:lectureId/:courseId/:id', function(req, res, next) {
      let query = {
        lecture_id: req.params.lectureId,
        course_id: req.params.courseId
    
      };
      let update = { $push: { message: { _id: req.params.id,likes: 1 } } }
    
      var options = {
        new: true
      }
      console.log('Liked', query)
    
      Chat.update(query, update, function(err, result) {
        if (err) {
          //res.send(err);
          console.log('err',err );
          return err;
        }
        res.json(result);
      })
    }); 

Its basically adding new document. I am stuck here how can i do this.

2 Answers 2

1
  "lecture_id": "451",
  "course_id": "5002"
},
{
  $inc: {
    "message.$[elem].likes": 1
  }
},
{
  new: true,
  arrayFilters: [
    {
      "elem._id": "5f980a84c4b0d52950bdf90a"
    }
  ]
})```
Sign up to request clarification or add additional context in comments.

Comments

0

Here is the mongo query.

db.collection.update({
  "lecture_id": "451",
  "course_id": "5002",
  "message._id": "5f980a84c4b0d52950bdf91a"
},
{
  "$inc": {
    "message.$.likes": 1
  }
})

Try it here Mongo Playground

Not an expert in nodejs. Below update statement may work for you.

let update = { $inc: { "message.$.likes": 1 } }

7 Comments

Thanx @wak786 its working as you said but its not what i am looking for. this is adding likes : 1 in every element of message array. but i want to update by adding 1 in likes value with respect to message._id. basically i have like button in my application and when i hit like button with respect to the message it should addon 1 in likes : 1 or like:2 and so on. with respect to the message id. not to all messages .
woohaaa ! finally its working for me . thanx @wak786 a lot your answer give me adirection. The following works for me. ` let update = { $inc: { "message.$[elem].likes" :1 } } var options = { new: true, arrayFilters: [ { "elem._id": req.params.id } ] }`
Cool.. Posted an update to answer. Also just saw your answer too. Looks good.
ya thats also looks good. now i am curious about which solution is better performance wise. bcz i am so new in Mongo so pleas suggest. is my query correct. is it the right way to do the same. ? @wak786
your query is correct. Only thing different is, in my query update operation already knows which object to update based on filter. In yours it only gets to know that after checking in arrayFilters. Both should work fine in normal workload. I am not not 100% sure but mine can be more performant if you have proper index.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.