1

I have a sample Post collection

[{
    "_id" : ObjectId("5da832caeb173112348e509b"),
    "body" : "Lorem Ipsu.",
    "comments" : [ 
        {
            "comment" : "my sample comment",
            "_id" : ObjectId("5db06e11d0987d0aa2cd5593"),
            "replies" : [ 
                {
                    "likes" : [ 
                        "5d999578aeb073247de4bd6e", 
                        "5da85558886aee13e4e7f044"
                    ],

                    "_id" : ObjectId("5db6a88f7c6cfb0d0c2b689b"),
                    "reply" : "my reply to this comment",
                    "user" : "5da85558886aee13e4e7f044"
                }
            ],

            "likes" : [ 
                "5da85558886aee13e4e7f044",
            ]
        }, 

    ],

}]

To add or remove element into comments.likes I can do like this,$push or $pull, it works with this below operation.

   Post.updateOne(
     {_id: req.body.id_post, "comments._id": req.body.id_comment},
     { $push: {"comments.$.likes": req.user._id}});

But if I want to push into replies.likes, could anyone please help to guide me?

I have tried something like this, but replies.likes is not updated.

 db.posts.updateOne(
    { "comments.replies._id": Object("5db6a88f7c6cfb0d0c2b689b") },
    { "$push": { "comments.0.replies.$.likes": Object("5db6a88f7c6cfb0d0c2b689a") } },
    function(err,numAffected) {
       // something with the result in here
    }
);
tree em
  • 20,379
  • 30
  • 92
  • 130
  • Possible dupe of https://stackoverflow.com/q/23577123/2683814 – s7vr Oct 28 '19 at 11:36
  • yes, I have try not not work, could you please help , I updated my trying. – tree em Oct 28 '19 at 11:55
  • IMO, This is not an optimal schema design. A document size can be max 18MB. But, comments array might grow infinite times. – Shihab Oct 28 '19 at 12:25
  • @Dijkstra what you mean? – tree em Oct 28 '19 at 13:49
  • @kn3l In mongodb, a document size can be maximum of 18MB AFAIK. So, lets say your blog post is very popular and receive hundreds of comments each day. Then, your schema design won't work. You have to create separate collection to store comments. – Shihab Oct 28 '19 at 13:52
  • but how if the comment will maximum of 18MB too, no? – tree em Oct 28 '19 at 14:21
  • In MongoDB document size limit **16 MB**, _not_ **18 MB**: [Document size limit](https://docs.mongodb.com/manual/core/document/index.html#document-size-limit). – prasad_ Oct 28 '19 at 14:34

1 Answers1

1

you can use array filter :

db.getCollection("test").updateOne(
  {
    "comments.replies": {
      "$elemMatch": {
        "_id": Object("5db6a88f7c6cfb0d0c2b689b")
      }
    }
  },
  { "$set": { "comments.$[outer].reply.$[inner].likes": Object("5db6a88f7c6cfb0d0c2b689a") } },
  {
    "arrayFilters": [
      { "outer._id": ObjectId("5cd26a886458720f7a66a413") },
      { "inner._id": ObjectId("5cd26a886458720f7a66a415") }
    ]
  }
)

don't mind ids

Babak Abadkheir
  • 2,222
  • 1
  • 19
  • 46
  • arrayFilters and these 2 ids `5cd26a886458720f7a66a413` , `5cd26a886458720f7a66a415` can be any ids , right? – tree em Oct 29 '19 at 01:31
  • I try still cannot push to replies.likes – tree em Oct 29 '19 at 02:37
  • @kn3l remember that the logic of MongoDB is based on _id .every document and subdocument will have its own unique _id .because of addressing .when you use array filters, you are telling mongo I want to affect this part of document. – Babak Abadkheir Oct 29 '19 at 10:12
  • @kn3l I had your problem , check this :https://stackoverflow.com/questions/56035093/mongodb-query-update-select-nested-fields – Babak Abadkheir Oct 29 '19 at 13:30
  • @kn3l using operators doenot matter its just how to target your documents in your db. – Babak Abadkheir Oct 30 '19 at 14:00