如何在同一个 mongodb 查询中使用 $unwind 然后使用 $group

发布于 01-18 10:08 字数 1134 浏览 1 评论 0原文

我有以下mongodb结构...

[
    {
        track: 'Newcastle',
        time: '17:30',
        date: '22/04/2022',
        bookmakers: [
            {
                bookmaker: 'Coral',
                runners: [
                    {
                        runner: 'John',
                        running: true,
                        odds: 3.2
                    },
                    ...
                ]
            },
            ...
        ]
    },
    ...
]

我正在尝试找到每个文档的登录机构阵列,以包括匹配指定的博彩公司值的对象,例如:

{ 'bookmakers.bookmaker': { $in: ['Coral', 'Bet365'] } }

目前,我正在使用以下mongodb查询来只有选择指定的庄家,但是我需要在文档被“ $ undind”分开后将其放回原处,有没有办法使用$组来做到这一点?

await HorseRacingOdds.aggregate([
    { $unwind: "$bookmakers" },
    {
        $group: {
            _id: "$_id",
            bookmakers: "$bookmakers"
        }
    },
    {
        $project: {
            "_id": 0,
            "__v": 0,
            "lastUpdate": 0
        }
    }
])

I have the following mongodb structure...

[
    {
        track: 'Newcastle',
        time: '17:30',
        date: '22/04/2022',
        bookmakers: [
            {
                bookmaker: 'Coral',
                runners: [
                    {
                        runner: 'John',
                        running: true,
                        odds: 3.2
                    },
                    ...
                ]
            },
            ...
        ]
    },
    ...
]

I'm trying to find filter the bookmakers array for each document to only include the objects that match the specified bookmaker values, for example:

{ 'bookmakers.bookmaker': { $in: ['Coral', 'Bet365'] } }

At the moment, I'm using the following mongodb query to only select the bookmakers that are specified, however I need to put the documents back together after they've been seperated by the '$unwind', is there a way I can do this using $group?

await HorseRacingOdds.aggregate([
    { $unwind: "$bookmakers" },
    {
        $group: {
            _id: "$_id",
            bookmakers: "$bookmakers"
        }
    },
    {
        $project: {
            "_id": 0,
            "__v": 0,
            "lastUpdate": 0
        }
    }
])

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

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

发布评论

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

评论(1

请叫√我孤独2025-01-25 10:08:40

带有 $filter 的普通 $addFields 怎么样?

db.collection.aggregate([
  {
    "$addFields": {
      "bookmakers": {
        "$filter": {
          "input": "$bookmakers",
          "as": "b",
          "cond": {
            "$in": [
              "$b.bookmaker",
              [
                "Coral",
                "Bet365"
              ]
            ]
          }
        }
      }
    }
  },
  {
    $project: {
      "_id": 0,
      "__v": 0,
      "lastUpdate": 0
    }
  }
])

这是 Mongo Playground 供您参考。

How about a plain $addFields with $filter?

db.collection.aggregate([
  {
    "$addFields": {
      "bookmakers": {
        "$filter": {
          "input": "$bookmakers",
          "as": "b",
          "cond": {
            "$in": [
              "$b.bookmaker",
              [
                "Coral",
                "Bet365"
              ]
            ]
          }
        }
      }
    }
  },
  {
    $project: {
      "_id": 0,
      "__v": 0,
      "lastUpdate": 0
    }
  }
])

Here is the Mongo playground for your reference.

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