MongoDB中阵列对象数组中的项目字段

发布于 2025-02-06 06:43:30 字数 2053 浏览 0 评论 0原文

我有此文档:

[
  {
    "name": "Report1",
    "specifications": [
      {
        "parameters": [
          {
            "name": "feature",
            "value": [
              "13"
            ]
          },
          {
            "name": "security",
            "value": [
              "XXXX-695"
            ]
          },
          {
            "name": "imageURL",
            "value": [
              "football.jpg"
            ],
            
          }
        ]
      }
    ]
  },
  {
    "name": "Report2",
    "specifications": [
      {
        "parameters": [
          {
            "name": "feature",
            "value": [
              "67"
            ]
          },
          {
            "name": "imageURL",
            "value": [
              "basketball.jpg"
            ],
            
          },
          {
            "name": "security",
            "value": [
              "XXXX-123"
            ]
          }
        ]
      }
    ]
  }
]

我想获得规格[0] .parameters.value [0]其中parameters.name =“ imageUrl”。这样:

[
  {
    "imageparam": "football.jpg",
    "name": "Report1"
  },
  {
    "imageparam": "basketball.jpg",
    "name": "Report2"
  }
]

我将MongoDB 3.6.3与MongoDB指南针一起使用。我想使用聚合(在MongoDB指南针中添加管道),以便最后写入此汇总,但它具有5美元的项目阶段。是否有更有效或更好的解决方案:

db.collection.aggregate([{$project: {
  _id: 0,
  name: 1,
  specifications: {$arrayElemAt: ["$specifications", 0]}
}}, {$project: {
  name: 1,
  imageparam: {
    $filter: {
      input: '$specifications.parameters',
      as: 'param',
      cond: {
        $eq: [
          '$$param.name',
          'imageURL'
        ]
      }
    }
  }
}}, {$project: {
  name: 1,
    imageparam: {$arrayElemAt: ["$imageparam",0]}
}}, {$project: {
  name: 1,
    imageparam: "$imageparam.value"
}}, {$project: {
  name: 1,
  imageparam: {$arrayElemAt: ["$imageparam",0]}
}}])

这是 Playground

I have this document:

[
  {
    "name": "Report1",
    "specifications": [
      {
        "parameters": [
          {
            "name": "feature",
            "value": [
              "13"
            ]
          },
          {
            "name": "security",
            "value": [
              "XXXX-695"
            ]
          },
          {
            "name": "imageURL",
            "value": [
              "football.jpg"
            ],
            
          }
        ]
      }
    ]
  },
  {
    "name": "Report2",
    "specifications": [
      {
        "parameters": [
          {
            "name": "feature",
            "value": [
              "67"
            ]
          },
          {
            "name": "imageURL",
            "value": [
              "basketball.jpg"
            ],
            
          },
          {
            "name": "security",
            "value": [
              "XXXX-123"
            ]
          }
        ]
      }
    ]
  }
]

I want to obtain specifications[0].parameters.value[0] where parameters.name = "imageUrl". Like that:

[
  {
    "imageparam": "football.jpg",
    "name": "Report1"
  },
  {
    "imageparam": "basketball.jpg",
    "name": "Report2"
  }
]

I use MongoDB 3.6.3 with MongoDB Compass. I want to use aggregation (to add a pipeline in MongoDb Compass) so I could write finally this aggregation but it has 5 $project stage. Is there any more efficient or better solution:

db.collection.aggregate([{$project: {
  _id: 0,
  name: 1,
  specifications: {$arrayElemAt: ["$specifications", 0]}
}}, {$project: {
  name: 1,
  imageparam: {
    $filter: {
      input: '$specifications.parameters',
      as: 'param',
      cond: {
        $eq: [
          '$param.name',
          'imageURL'
        ]
      }
    }
  }
}}, {$project: {
  name: 1,
    imageparam: {$arrayElemAt: ["$imageparam",0]}
}}, {$project: {
  name: 1,
    imageparam: "$imageparam.value"
}}, {$project: {
  name: 1,
  imageparam: {$arrayElemAt: ["$imageparam",0]}
}}])

This is playground.

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

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

发布评论

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

评论(1

阳光下慵懒的猫 2025-02-13 06:43:31

您可以将其减少到2个阶段,可能还有其他选项,

  • 直接通过$ arrayElemat ecementions.parameters.parameters $ filter输入并找到imageUrl使用的匹配值
  • 可以使用$ addfields$ set阶段以获取返回结果的第一个元素
db.collection.aggregate([
  {
    $project: {
      _id: 0,
      name: 1,
      imageparam: {
        $arrayElemAt: [
          {
            $filter: {
              input: { $arrayElemAt: ["$specifications.parameters", 0] },
              cond: { $eq: ["$this.name", "imageURL"] }
            }
          },
          0
        ]
      }
    }
  },
  {
    $addFields: {
      imageparam: { $arrayElemAt: ["$imageparam.value", 0] }
    }
  }
])

您可以在单个阶段使用$ let绑定变量的第二个选项用于在指定的表达式中使用,

db.collection.aggregate([
  {
    $project: {
      _id: 0,
      name: 1,
      imageparam: {
        $let: {
          vars: {
            param: {
              $arrayElemAt: [
                {
                  $filter: {
                    input: { $arrayElemAt: ["$specifications.parameters", 0] },
                    cond: { $eq: ["$this.name", "imageURL"] }
                  }
                },
                0
              ]
            }
          },
          in: { $arrayElemAt: ["$param.value", 0] }
        }
      }
    }
  }
])

playground

You can reduce it to 2 stages, might be there will be other options as well,

  • pass directly $arrayElemAt of specifications.parameters to $filter input and find the matching value for imageURL
  • use can use $addFields or $set stage to get first element from return result
db.collection.aggregate([
  {
    $project: {
      _id: 0,
      name: 1,
      imageparam: {
        $arrayElemAt: [
          {
            $filter: {
              input: { $arrayElemAt: ["$specifications.parameters", 0] },
              cond: { $eq: ["$this.name", "imageURL"] }
            }
          },
          0
        ]
      }
    }
  },
  {
    $addFields: {
      imageparam: { $arrayElemAt: ["$imageparam.value", 0] }
    }
  }
])

Playground

The second option you can do it in single stage using $let to bind the variables for use in the specified expression,

db.collection.aggregate([
  {
    $project: {
      _id: 0,
      name: 1,
      imageparam: {
        $let: {
          vars: {
            param: {
              $arrayElemAt: [
                {
                  $filter: {
                    input: { $arrayElemAt: ["$specifications.parameters", 0] },
                    cond: { $eq: ["$this.name", "imageURL"] }
                  }
                },
                0
              ]
            }
          },
          in: { $arrayElemAt: ["$param.value", 0] }
        }
      }
    }
  }
])

Playground

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