如何在不使用unwind的情况下匹配mongodb中的数组字段和嵌套数组字段

发布于 2025-01-11 07:12:30 字数 855 浏览 0 评论 0原文

我的 json 数据将如下所示,

{
  "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"
        }
      ]
    }
  ]
}

我需要匹配下面提到的一些条件:

  1. 匹配“videoId”==“29”
  2. 然后匹配“videoComments.deletedbyowner”==“FALSE”
  3. 如果我匹配第二个条件那么我需要匹配 "videoComments.replyComments.replyDeletedbyowner" == "FALSE"

我不能使用 unwind,因为我的老板告诉我 unwind 是一项成本高昂的操作,它会影响应用程序的性能。因此,在不使用 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

琴流音 2025-01-18 07:12:30

查询

  • 此命令仅保留带有 videoId=29 的文档,
  • 仅保留带有 deletedbyowner="FALSE" 的元素
  • ,并且仅保留带有 replyDeletedbyowner="FALSE" 的元素

*这是聚合解决方案,我们还有 $elemMatch$ 来投影匹配的元素,但这里您需要在嵌套数组上匹配,所以我认为您需要聚合,但我是不完全确定。

在此处测试代码

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]}}}}}])

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]}}}}}])
堇年纸鸢 2025-01-18 07:12:30
collection_name.find(
  { videoId : 29 },
  { videoComments : { $elemMatch : { deletedbyowner : "FALSE", $elemMatch: { replyDeletedbyowner: "FALSE"} } }
).pretty();

我想这就是您正在寻找的。有关更多信息,请查看此doc

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文