1

So this is how my document looks like right now. And what I am trying to do is to push an element to an array which is located comments -> comment -> replies -> reply -> likes

{
    _id: "5afa9d1a20baf95e03921a10",
    comments: [
        {
            commentID: "67917200-67ca-11e8-85f8-49f00c21219a",
            likes: [ ],
            dislikes: [ ],
            replies: [
                {
                    replyID: "6c6e41e0-67ca-11e8-85f8-49f00c21219a",
                    likes: [
                        // this is the array I would like to update
                    ],
                    dislikes: [ ]
                }
            ]
        }
    ]
},

I am successful when updating one level nested array but I do not understand why I cannot update this array the same way.

Right now my query looks like this.

db.collection.update(
        { 
          "_id" : _id, 
          "comments.commentID" : cid, 
          "comments.replies.replyID" : rid },
        { 
          $push : { "comments.replies.$.likes" : uid } 
        }
      )

When trying to send a request with Postman it just freezes and I have to cancel the request.

Jakob Valik
  • 41
  • 1
  • 6
  • You cannot traverse that many arrays with the positional `$` operator, and it's needed on the first array anyway. You really should not be nesting arrays like this even if you have a MongoDB with actual support for atomic updates. You can use absolute indexes, but you are better off "flattening" the array structure. Read the linked answer for all the details. – Neil Lunn Jun 04 '18 at 08:51

0 Answers0