'how to match array field and nested array field in mongodb without using unwind

My json data will look like this

{
  "videoId": 29,
  "videoComments": [
    {
      "comment": "awsome",
      "userId": 15,
      "commentTime": "2022-03-01T12:37:49.734Z",
      "userName": "user1646127068323",
      "deletedbyowner": "FALSE",
      "_id": "621e139d8e4195079c86488",
      "replyComments": [
        {
          "replyComment": "thank you",
          "replyCommentTime": "2022-03-01T12:44:53.193Z",
          "replyDeletedbyowner": "FALSE",
          "_id": "621e154557fa7045e342540"
        }
      ]
    }
  ]
}

I need to match some conditions that I mentioned below :

  1. match "videoId" == "29"
  2. then match "videoComments.deletedbyowner" == "FALSE"
  3. if I match second condition then I need to match "videoComments.replyComments.replyDeletedbyowner" == "FALSE"

I can't use unwind because my boss told me that unwind is a costly operation it will effect the app performance. so with out using unwind I need to match these conditions. could you please help out of this.



Solution 1:[1]

collection_name.find(
  { videoId : 29 },
  { videoComments : { $elemMatch : { deletedbyowner : "FALSE", $elemMatch: { replyDeletedbyowner: "FALSE"} } }
).pretty();

I think this is what you are looking for. For more info check this doc

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shivam Mishra