如何使用MongoDB中的数组嵌套过滤器获取特定的字段值?

发布于 2025-02-01 11:07:32 字数 931 浏览 3 评论 0原文

在滤波嵌套数组值之后,我添加了“ $ project”查询以选择特定字段。

我想在嵌套数组之外获得“ new_yn”字段值,但我只有ID和嵌套数组值。

我该如何修复查询?你会给我一些指南吗? (MongoDB版本是5.0.6社区)

[QUERY]

db.collection.aggregate([
  {
    $match: {
      "new_yn": "Y",
      "port_info": {
        $elemMatch: {
          "port_code": "http_https"
        }
      }
    }
  },
  {
    $project: {
      port_info: {
        $filter: {
          input: "$port_info",
          as: "item",
          cond: {
            $eq: [
              "$$item.port_code",
              "http_https"
            ]
          }
        }
      }
    }
  },
  {
    "$project": {
      "_id": 1,
      "new_yn": 1,
      "port_info.ip_port": 1
    }
  }
])

[Mongo Playground]

https://mongoplayground.net/p/oxoaz8ct4ka

After I filtered nested array values then I added "$project" query for selecting specific fields.

I want to get "new_yn" field values outside of the nested array but I only got id and nested array values.

How can I fix my Query? Would you give me some guides?
(MongoDB version is 5.0.6 Community)

[Query]

db.collection.aggregate([
  {
    $match: {
      "new_yn": "Y",
      "port_info": {
        $elemMatch: {
          "port_code": "http_https"
        }
      }
    }
  },
  {
    $project: {
      port_info: {
        $filter: {
          input: "$port_info",
          as: "item",
          cond: {
            $eq: [
              "$item.port_code",
              "http_https"
            ]
          }
        }
      }
    }
  },
  {
    "$project": {
      "_id": 1,
      "new_yn": 1,
      "port_info.ip_port": 1
    }
  }
])

[Mongo Playground]

https://mongoplayground.net/p/OXOaz8ct4KA

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

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

发布评论

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

评论(1

御弟哥哥 2025-02-08 11:07:32

这是因为您还没有将其包括在第一个$ project阶段。

只需将new_yn:1添加到第一个项目阶段,它将起作用。

db.collection.aggregate([
  {
    $match: {
      "new_yn": "Y",
      "port_info": {
        $elemMatch: {
          "port_code": "http_https"
        }
      }
    }
  },
  {
    $project: {
      new_yn: 1,
      port_info: {
        $filter: {
          input: "$port_info",
          as: "item",
          cond: {
            $eq: [
              "$item.port_code",
              "http_https"
            ]
          }
        }
      }
    }
  },
  {
    "$project": {
      "_id": 1,
      "new_yn": 1,
      "port_info.ip_port": 1
    }
  }
])

建议由伯利恒,您可以使用 $ set 也将带来该文档中的所有其他字段,我相信您不想要。

This is because you haven't included it in the first $project stage.

Just add new_yn:1 to the first project stage and it will work.

db.collection.aggregate([
  {
    $match: {
      "new_yn": "Y",
      "port_info": {
        $elemMatch: {
          "port_code": "http_https"
        }
      }
    }
  },
  {
    $project: {
      new_yn: 1,
      port_info: {
        $filter: {
          input: "$port_info",
          as: "item",
          cond: {
            $eq: [
              "$item.port_code",
              "http_https"
            ]
          }
        }
      }
    }
  },
  {
    "$project": {
      "_id": 1,
      "new_yn": 1,
      "port_info.ip_port": 1
    }
  }
])

As suggested by Bethlee, you can use $set as well, but it will bring all other fields from that document, which I believe you dont want.

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