You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MongoDB如何查询同一section下嵌套数组满足多条件的文档

错误原因

你写的查询语句中,在同一个questions条件对象下重复声明了$elemMatch键,JSON语法中重复键会被后声明的覆盖,因此实际生效的只有q为size且a为large的校验逻辑,导致第二个文档也被命中——第二个文档的section1满足size条件,section2满足color前缀条件,刚好匹配了被覆盖后的松散逻辑。

最优解决方案(无需聚合,直接使用find查询)

直接在$elemMatch内层用$and包裹两个独立的questions校验条件,即可保证两个规则必须在同一个section下同时生效:

{
  "sections": {
    "$elemMatch": {
      "$and": [
        {
          "questions": {
            "$elemMatch": {
              "q": "color",
              "a": { "$regex": "^b" }
            }
          }
        },
        {
          "questions": {
            "$elemMatch": {
              "q": "size",
              "a": "large"
            }
          }
        }
      ]
    }
  }
}

逻辑说明

该查询要求sections数组中至少存在一个section,同时满足两个规则:

  • 该section的questions数组中存在q为color、a以b开头的元素
  • 同一个section的questions数组中存在q为size、a为large的元素

测试下来只会命中你给出的第一份文档,完全符合预期。

可选聚合方案(如需过滤掉不匹配的section)

如果你需要在返回结果中直接剔除掉文档内不满足条件的section,保留原始文档结构,可以用如下聚合语句,无需手动重构:

[
  {
    "$match": {
      "sections": {
        "$elemMatch": {
          "$and": [
            {
              "questions": {
                "$elemMatch": {
                  "q": "color",
                  "a": { "$regex": "^b" }
                }
              }
            },
            {
              "questions": {
                "$elemMatch": {
                  "q": "size",
                  "a": "large"
                }
              }
            }
          ]
        }
      }
    }
  },
  {
    "$addFields": {
      "sections": {
        "$filter": {
          "input": "$sections",
          "cond": {
            "$and": [
              {
                "$gt": [
                  {
                    "$size": {
                      "$filter": {
                        "input": "$$this.questions",
                        "cond": {
                          "$and": [
                            { "$eq": ["$$this.q", "color"] },
                            { "$regexMatch": { "input": "$$this.a", "regex": "^b" } }
                          ]
                        }
                      }
                    }
                  },
                  0
                ]
              },
              {
                "$gt": [
                  {
                    "$size": {
                      "$filter": {
                        "input": "$$this.questions",
                        "cond": {
                          "$and": [
                            { "$eq": ["$$this.q", "size"] },
                            { "$eq": ["$$this.a", "large"] }
                          ]
                        }
                      }
                    }
                  },
                  0
                ]
              }
            ]
          }
        }
      }
    }
  }
]

内容的提问来源于stack exchange,提问作者Claus Nielsen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 05:06:07