【问题标题】:Filter result in mongo sub-sub array在 mongo 子子数组中过滤结果
【发布时间】:2021-06-17 13:33:31
【问题描述】:

我有一些我们这样的收藏:

[{
  "_id": ObjectId("604f3ae3194f2135b0ade569"),
  "parameters": [
    {
      "_id": ObjectId("602b7455f4b4bf5b41662ec1"),
      "name": "Purpose",
      "options": [
        {
          "id": ObjectId("602b764ff4b4bf5b41662ec2"),
          "name": "debug",
          "sel": false
        },
        {
          "id": ObjectId("602b767df4b4bf5b41662ec3"),
          "name": "performance",
          "sel": false
        },
        {
          "id": ObjectId("602b764ff4b4bf5b41662ec4"),
          "name": "security",
          "sel": false
        },
        {
          "id": ObjectId("602b767df4b4bf5b41662ec5"),
          "name": "Not Applicable",
          "sel": false
        }
      ],
      "type": "multiple"
    },
    {
      "_id": ObjectId("602b79d35d4a1333b8b6e5ba"),
      "name": "Organization",
      "options": [
        {
          "id": ObjectId("602b79d353c89933b8238325"),
          "name": "SW",
          "sel": false
        },
        {
          "id": ObjectId("602b79d353c89933b8238326"),
          "name": "HW",
          "sel": false
        }
      ],
      "type": "multiple"
    }
  ]
}]

参数最多30个。

我需要在 mongo 中实现一个“过滤”集合。

如果我过滤一个或多个parameters._id,mongo 返回:

  1. this parameters._idoptions.sel 匹配的集合_id
  2. 所有options.sel 等于falsethis parameters._id 的集合_id
  3. 如果parameters._id 已将options.name:"Not Applicable" 设置为值options.sel:true,则不返回集合_id

例如,如果我匹配parameters._id:ObjectId("602b7455f4b4bf5b41662ec1") 和这个parameters.options.id:ObjectId("602b764ff4b4bf5b41662ec2"),我期望:

  • 不是集合_id,对于parameters._id:ObjectId("602b7455f4b4bf5b41662ec1"),特定parameters.options.id: ObjectId("602b767df4b4bf5b41662ec5") 的值options.sel:true
  • 与查询匹配的所有集合_id
  • 所有集合_id,对于parameters._id:ObjectId("602b7455f4b4bf5b41662ec1"),所有特定的parameters.options.sel:false

接下来我需要为更多参数制定这条规则。

我想为每个规则实现三个聚合... 你有什么建议吗?

【问题讨论】:

  • 能否请您包括预期的输出。

标签: mongodb mongodb-query


【解决方案1】:

试试这个查询:

db.testCollection.aggregate([
    { $unwind: "$parameters" },
    {
        $match: {
            "parameters._id": ObjectId("602b7455f4b4bf5b41662ec1"),
            "parameters.options.id": ObjectId("602b767df4b4bf5b41662ec5")
        }
    },
    {
        $addFields: {
            options: {
                $filter: {
                    input: "$parameters.options",
                    as: "option",
                    cond: {
                        $and: [
                            { $ne: ["$$option.sel", true] },
                            { $ne: ["$$option.name", "Not Applicable"] }
                        ]
                    }
                }
            }
        }
    }
])

【讨论】:

  • 嗨@dheemanth-bhat,结果如预期,the_id 就足够了,但没关系。但也有一些问题: 1.parameters.options.id 与各自的parameters.options.sel:true 匹配 2.如果参数不适用,则"$$option.name": "Not Applicable" need to match with respective "$$option.sel:true"```如果设置了。跨度>
【解决方案2】:

有了@dheemanth-bath 的想法, 我做了这个查询:

db.testCollection.aggregate([
    { $unwind: "$parameters" },
    {
        $match: {
            "parameters._id": ObjectId("602b7455f4b4bf5b41662ec1"),
        }
    },
    {
        $addFields: {
            match: {
                $filter: {
                    input: "$parameters.options",
                    as: "option",
                    cond: {
                        $and: [
                           { $eq: ["$$option.id", ObjectId('602b767df4b4bf5b41662ec5')] },
                           { $eq: ["$$option.sel", true] }
                        ]
                    }
                }
            },
            notDeclared: {
                $filter: {
                    input: "$parameters.options",
                    as: "option",
                    cond: {
                        $and: [
                           { $eq: ["$$option.name", "Not Applicable"]},
                           { $eq: ["$$option.sel", true] }
                        ]
                    }
                }
            }
        }
    }
])

思路是:查询后count个notDeclared的元素。如果 > 0 则浪费集合。否则计算匹配数,如果>0,则至少有一个元素可以匹配。

很好。但是我如何评估 options.sel 的 所有 元素是否为假?

如果我检查另一个参数,我需要进行另一个聚合(一个用于参数)?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    • 1970-01-01
    相关资源
    最近更新 更多