在数组的另一个对象中获取数组的猫鼬对象

发布于 2025-02-12 10:21:49 字数 1549 浏览 1 评论 0原文

我在MongoDB系列中有此文档:

        "_id": "62be0271d373b2f2fc1826a2",
        "condition": "new",
        "variants": [
            {
                "color_name": "Green",
                "items": [
                    {
                        "size": "S",
                        "price": 100,
                        "quantity": 1,
                        "_id": "62be0271d373b2f2fc1826a4"
                    },
                    {
                        "size": "M",
                        "price": 100,
                        "quantity": 2,
                        "_id": "62be0271d373b2f2fc1826a5"
                    }
                ],
                "_id": "62be0271d373b2f2fc1826a3"
            },
            {
                "color_name": "Blue",
                "items": [
                    {
                        "size": "S",
                        "price": 100,
                        "quantity": 1,
                        "_id": "62be0271d373b2f2fc1826a7"
                    },
                    {
                        "size": "S",
                        "price": 100,
                        "quantity": 1,
                        "_id": "62be0271d373b2f2fc1826a8"
                    }
                ],
                "_id": "62be0271d373b2f2fc1826a6"
            }
        ],
        "featured": true

我只想获得第一个具有_id =“ 62BE0271D373B2F2F2F2FC1826A3”的变体,还有第二个具有此“ _id”的项目:“ _id”:“ 62be0271d373b2f2f2f2f2f2f2f2f2fc1826aa5”,也不显示其他字段。

注意:集合中有更多类似的对象,因此循环并匹配所有内容并仅检索匹配的字段。

I have this document in my mongodb collection:

        "_id": "62be0271d373b2f2fc1826a2",
        "condition": "new",
        "variants": [
            {
                "color_name": "Green",
                "items": [
                    {
                        "size": "S",
                        "price": 100,
                        "quantity": 1,
                        "_id": "62be0271d373b2f2fc1826a4"
                    },
                    {
                        "size": "M",
                        "price": 100,
                        "quantity": 2,
                        "_id": "62be0271d373b2f2fc1826a5"
                    }
                ],
                "_id": "62be0271d373b2f2fc1826a3"
            },
            {
                "color_name": "Blue",
                "items": [
                    {
                        "size": "S",
                        "price": 100,
                        "quantity": 1,
                        "_id": "62be0271d373b2f2fc1826a7"
                    },
                    {
                        "size": "S",
                        "price": 100,
                        "quantity": 1,
                        "_id": "62be0271d373b2f2fc1826a8"
                    }
                ],
                "_id": "62be0271d373b2f2fc1826a6"
            }
        ],
        "featured": true

I want to get only the first variant with it's _id = "62be0271d373b2f2fc1826a3" and also with it's second items that has this "_id": "62be0271d373b2f2fc1826a5", don't show other fields.

NOTE: there are more objects like this in the collection so loop through and match all and retrieve only fields matched.

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

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

发布评论

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

评论(1

中性美 2025-02-19 10:21:49

一个选项是使用$ filter

db.collection.aggregate([
  {
    $project: {
      variants: {
        $first: {
          $filter: {
            input: "$variants",
            cond: {
              $eq: [
                "$this._id",
                "62be0271d373b2f2fc1826a3"
              ]
            }
          }
        }
      },
      _id: 0
    }
  },
  {
    $set: {
      "variants.items": {
        $filter: {
          input: "$variants.items",
          cond: {
            $eq: [
              "$this._id",
              "62be0271d373b2f2fc1826a5"
            ]
          }
        }
      },
      _id: "$REMOVE"
    }
  }
])

请参阅 Playground示例

One option is using $filter:

db.collection.aggregate([
  {
    $project: {
      variants: {
        $first: {
          $filter: {
            input: "$variants",
            cond: {
              $eq: [
                "$this._id",
                "62be0271d373b2f2fc1826a3"
              ]
            }
          }
        }
      },
      _id: 0
    }
  },
  {
    $set: {
      "variants.items": {
        $filter: {
          input: "$variants.items",
          cond: {
            $eq: [
              "$this._id",
              "62be0271d373b2f2fc1826a5"
            ]
          }
        }
      },
      _id: "$REMOVE"
    }
  }
])

See how it works on the playground example

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