1

My collection is like this: https://mongoplayground.net/p/91InBXrUq7R

With this query I can update replies.likes

db.getCollection("posts").updateOne(

{
    "_id": ObjectId("5da832caeb173112348e509b"), //posts._id
    "comments.replies._id":ObjectId("5db6a88f7c6cfb0d0c2b689b"),//replies._id
  },
  { "$push": { "comments.$[outer].replies.$[inner].likes": "10000012" } },
  {
    "arrayFilters": [
      { "outer._id": ObjectId("5db06e11d0987d0aa2cd5593") },//comments._id
      { "inner._id": ObjectId("5db6a88f7c6cfb0d0c2b689b") }//replies._id
    ]
  }
)

But when I code using mongoose, express, collection not update

    //Like Reply toggle
router.post("/toggleLikeReply", function(req, res, next) {
  var id_post = req.body.id_post;
  var id_comment = req.body.id_comment;
  var id_reply = req.body.id_reply;
  var id_user = req.user._id;
  console.log("id_post: "+id_post+" id_comment: "+id_comment+" id_reply: "+id_reply+" id_user: "+id_user);

  //todo
  Post.aggregate([
      { $match: {_id: ObjectId(id_post),"comments._id": ObjectId(id_comment)}},
      { $unwind: "$comments"},
      { $match: { "comments._id": ObjectId(id_comment)}},
      { $project: {"replies": "$comments.replies", _id: 0}},
      { $match: { "replies._id": ObjectId(id_reply)}},
      { $project: {"likes": "$replies.likes", _id: 0}}, 
    ]).exec((err, users_liked) => {
      var index = users_liked[0].likes[0].indexOf(id_user);
      console.log(users_liked[0].likes[0]);
      //todo
      if (index == -1) {
        const updatePost = async () => {
          try { 
            await Post.updateOne({
              _id: ObjectId(req.body.id_post), 
              "comments.replies._id": ObjectId(req.body.id_reply)},
              { $push: {"comments.$[outer].replies.$[inner].likes": ObjectId(req.user._id)} },
            {
              "arrayFilters": [
                { "outer._id": ObjectId(req.body.id_comment)  },
                { "inner._id": ObjectId(req.body.id_reply) }
              ]
            }
            );
          } catch (error) {
             console.log("error", error);
          }
        };
        updatePost().then(function(data) {res.send({  like: true, success: true})});

      }else{
        const updatePost = async () => {
          try { 
           await Post.updateOne({
          _id: ObjectId(req.body.id_post), 
          "comments.replies._id": ObjectId(req.body.id_reply)},
          { $pull: {"comments.$[outer].replies.$[inner].likes": ObjectId(req.user._id)} },
        {
          "arrayFilters": [
            { "outer._id": ObjectId(req.body.id_comment)  },
            { "inner._id": ObjectId(req.body.id_reply) }
          ]
        }
        );
          } catch (error) {
            console.log("", error);
          }
        };
        updatePost().then(function(data) {res.send({  like: false, success: true})});
      }


  })

});

I logged the all the id is come and the same as I did with mongo query directly .

id_post: 5da832caeb173112348e509b 
id_comment: 5db06e11d0987d0aa2cd5593 
id_reply: 5db6a88f7c6cfb0d0c2b689b 
id_user: 5da85558886aee13e4e7f044

What is wrong with my code using mongoose and express?

tree em
  • 20,379
  • 30
  • 92
  • 130

1 Answers1

1

Try This Query

var mongoose = require('mongoose');
const Schema = mongoose.Schema
const ObjectId = Schema.Types.ObjectId 
const updatePost = async () => {
          try { 
            await Post.updateOne({
              _id: ObjectId(req.body.id_post), 
              "comments.replies._id": ObjectId(req.body.id_reply)},
              { $push: {"comments.$[outer].replies.$[inner].likes": req.user._id} },
            {
              "arrayFilters": [
                { "outer._id": ObjectId(req.body.id_comment)  },
                { "inner._id": ObjectId(req.body.id_reply) }
              ]
            }
            );
          } catch (error) {
             console.log("error", error);
          }
        };
        updatePost().then(function(data) {res.send({  like: true, success: true})});
Mahesh Bhatnagar
  • 1,042
  • 1
  • 7
  • 8