如何在Mongo聚集管道中促进/拉动一个级别的孩子领域

发布于 2025-01-20 18:53:48 字数 2255 浏览 1 评论 0原文

我有一个如下所示的集合:

[
  {
    _id: ObjectId("6255a889fb63822bd51b0356"),
    answers: {
      "6255a889fb63822bd51b0326": {
        value: ObjectId("6255a889fb63822bd51b0329"),
        
      },
      "6255a889fb63822bd51b032a": {
        value: [
          ObjectId("6255a889fb63822bd51b0321"),
          ObjectId("6255a889fb63822bd51b032d")
        ],
        
      },
      "6255a889fb63822bd51b032e": {
        value: 2,
        comment: "hello",
        
      }
    },
    createdAt: ISODate("2022-03-12T17:27:53.890Z")
  }
]

我启动管道

{
  $project: {
     _id: 0,
     answers: { $objectToArray: "$answers" },
     date: { $dateToString: { format: dateFormat, date: "$createdAt" } },
   },
},

并得到如下结果:

[
  {
    answers: [
      {
        k: '6255a9a60fd8f862f4bd287f',
        v: {
          value: new ObjectId("6255a9a60fd8f862f4bd2882"),
          _id: new ObjectId("6255a9a60fd8f862f4bd28b2")
        }
      },
      {
        k: '6255a9a60fd8f862f4bd2883',
        v: {
          value: [
            new ObjectId("6255a9a60fd8f862f4bd287a"),
            new ObjectId("6255a9a60fd8f862f4bd2886")
          ],
          _id: new ObjectId("6255a9a60fd8f862f4bd28b3")
        }
      },
      {
        k: '6255a9a60fd8f862f4bd2887',
        v: {
          value: 2,
          comment: 'hello',
          _id: new ObjectId("6255a9a60fd8f862f4bd28b4")
        }
      }
    ],
    date: '2022-03-12'
  }
]

我想拉起 value 并将 v 替换为 value< /code> 所以我最终得到的结果如下所示。我在想也许有某种方法可以在 $objectToArray 的第一步中做到这一点?或者之后我还需要采取其他步骤吗?

[
  {
    answers: [
      {
        k: '6255a9a60fd8f862f4bd287f',
        v: new ObjectId("6255a9a60fd8f862f4bd2882")
      },
      {
        k: '6255a9a60fd8f862f4bd2883',
        v: [
            new ObjectId("6255a9a60fd8f862f4bd287a"),
            new ObjectId("6255a9a60fd8f862f4bd2886")
          ]
      },
      {
        k: '6255a9a60fd8f862f4bd2887',
        v: 2
      }
    ],
    date: '2022-03-12'
  }
]

I have a collection that looks like this:

[
  {
    _id: ObjectId("6255a889fb63822bd51b0356"),
    answers: {
      "6255a889fb63822bd51b0326": {
        value: ObjectId("6255a889fb63822bd51b0329"),
        
      },
      "6255a889fb63822bd51b032a": {
        value: [
          ObjectId("6255a889fb63822bd51b0321"),
          ObjectId("6255a889fb63822bd51b032d")
        ],
        
      },
      "6255a889fb63822bd51b032e": {
        value: 2,
        comment: "hello",
        
      }
    },
    createdAt: ISODate("2022-03-12T17:27:53.890Z")
  }
]

I start my pipeline with

{
  $project: {
     _id: 0,
     answers: { $objectToArray: "$answers" },
     date: { $dateToString: { format: dateFormat, date: "$createdAt" } },
   },
},

and get a result like this:

[
  {
    answers: [
      {
        k: '6255a9a60fd8f862f4bd287f',
        v: {
          value: new ObjectId("6255a9a60fd8f862f4bd2882"),
          _id: new ObjectId("6255a9a60fd8f862f4bd28b2")
        }
      },
      {
        k: '6255a9a60fd8f862f4bd2883',
        v: {
          value: [
            new ObjectId("6255a9a60fd8f862f4bd287a"),
            new ObjectId("6255a9a60fd8f862f4bd2886")
          ],
          _id: new ObjectId("6255a9a60fd8f862f4bd28b3")
        }
      },
      {
        k: '6255a9a60fd8f862f4bd2887',
        v: {
          value: 2,
          comment: 'hello',
          _id: new ObjectId("6255a9a60fd8f862f4bd28b4")
        }
      }
    ],
    date: '2022-03-12'
  }
]

I want to pull value up and replace v with value so I wind up with a result that looks like this. I was thinking maybe there's some way to do it in the first step of $objectToArray? or do I need another step afterwards?

[
  {
    answers: [
      {
        k: '6255a9a60fd8f862f4bd287f',
        v: new ObjectId("6255a9a60fd8f862f4bd2882")
      },
      {
        k: '6255a9a60fd8f862f4bd2883',
        v: [
            new ObjectId("6255a9a60fd8f862f4bd287a"),
            new ObjectId("6255a9a60fd8f862f4bd2886")
          ]
      },
      {
        k: '6255a9a60fd8f862f4bd2887',
        v: 2
      }
    ],
    date: '2022-03-12'
  }
]

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

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

发布评论

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

评论(1

丢了幸福的猪 2025-01-27 18:53:48

您只需要再一个 $map 来调整您想要的形式。

db.collection.aggregate([
  {
    $project: {
      _id: 0,
      answers: {
        "$map": {
          "input": {
            $objectToArray: "$answers"
          },
          "as": "a",
          "in": {
            k: "$a.k",
            v: "$a.v.value"
          }
        }
      },
      date: {
        $dateToString: {
          format: "%Y-%m-%d",
          date: "$createdAt"
        }
      }
    }
  }
])

这是 Mongo Playground 供您参考。

You just need one more $map to wrangle to your desired form.

db.collection.aggregate([
  {
    $project: {
      _id: 0,
      answers: {
        "$map": {
          "input": {
            $objectToArray: "$answers"
          },
          "as": "a",
          "in": {
            k: "$a.k",
            v: "$a.v.value"
          }
        }
      },
      date: {
        $dateToString: {
          format: "%Y-%m-%d",
          date: "$createdAt"
        }
      }
    }
  }
])

This is the Mongo playground for your reference.

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