0

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.

2
  • you want to keep the documents that match your creteria unchained? or you want to change the arrays also, to have only the members that much the creteria? Commented Mar 3, 2022 at 13:56
  • Is this what you are looking for? $elemMatch could be used when querying arrays. Commented Mar 3, 2022 at 14:02

2 Answers 2

1

Query

  • this keep the documents with videoId=29
  • only the elements with deletedbyowner="FALSE"
  • and only the elements with replyDeletedbyowner="FALSE"

*this is aggregate solution, we have also $elemMatch and $ to project the matched element, but here you need match on nested arrays, so i think you need aggregation, but i am not completly sure.

Test code here

aggregate(
[{"$match":{"$expr":{"$eq":["$videoId", 29]}}},
 {"$set":
  {"videoComments":
   {"$map":
    {"input":"$videoComments",
     "in":
     {"$cond":
      [{"$ne":["$$this.deletedbyowner", "FALSE"]}, null,
       {"$mergeObjects":
        ["$$this",
         {"replyComments":
          {"$filter":
           {"input":"$$this.replyComments",
            "cond":{"$eq":["$$reply.replyDeletedbyowner", "FALSE"]},
            "as":"reply"}}}]}]}}}}},
 {"$set":
  {"videoComments":
   {"$filter":
    {"input":"$videoComments", "cond":{"$ne":["$$this", null]}}}}}])
Sign up to request clarification or add additional context in comments.

2 Comments

this is perfectly working with my conditions thank you:)
@RoopeshKrishna ok good, if it works accept it as solution, this is for helping the one that answered, and for other people to know if answer worked and useful.
0
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

Comments

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.