【问题标题】:MongoDB Aggregate to make the nested aray as RootMongoDB Aggregate 将嵌套数组设为 Root
【发布时间】:2021-06-28 23:19:25
【问题描述】:

我从我的聚合中获取这个结构,每个项目都有一个库存项目数组。我想将嵌套的库存项目数组作为主要的 Root 结果。我添加了一些关于结果是什么以及我真正想要什么的演示。

[
    {
      "inventory": [
        {
          "_id": "603fb4d4dd0c9134c8ce59b7",
          "item_id": "60145c769bd3b700087af513",
          "createdAt": "2021-01-29T19:05:26.166Z",
          "updatedAt": "2021-03-27T09:46:07.300Z",
        },
        {
          "_id": "603fb4d4dd0c9134c8ce59b8",
          "item_id": "60145c769bd3b700087af513",
          "createdAt": "2021-01-29T19:05:26.166Z",
          "updatedAt": "2021-03-27T09:46:07.300Z",
        },
      ]
    },
    {
      "inventory": [
        {
          "_id": "603fb4d4dd0c9134c8ce59b9",
          "item_id": "60145c769bd3b700087af514",
          "createdAt": "2021-01-29T19:05:26.166Z",
          "updatedAt": "2021-03-27T09:46:07.300Z",
        },
      ]
    },
    {
      "inventory": [
        {
          "_id": "603fb4d4dd0c9134c8ce10",
          "item_id": "60145c769bd3b700087af515",
          "createdAt": "2021-01-29T19:05:26.166Z",
          "updatedAt": "2021-03-27T09:46:07.300Z",
        },
        {
          "_id": "603fb4d4dd0c9134c8ce7",
          "item_id": "60145c769bd3b700087af515",
          "createdAt": "2021-01-29T19:05:26.166Z",
          "updatedAt": "2021-03-27T09:46:07.300Z",
        },
      ]
    },
  ]
}

我想要这样的东西。

[
        {
          "_id": "603fb4d4dd0c9134c8ce59b7",
          "item_id": "60145c769bd3b700087af513",
          "createdAt": "2021-01-29T19:05:26.166Z",
          "updatedAt": "2021-03-27T09:46:07.300Z",
        },
        {
          "_id": "603fb4d4dd0c9134c8ce59b7",
          "item_id": "60145c769bd3b700087af513",
          "createdAt": "2021-01-29T19:05:26.166Z",
          "updatedAt": "2021-03-27T09:46:07.300Z",
        },
        {
          "_id": "603fb4d4dd0c9134c8ce59b7",
          "item_id": "60145c769bd3b700087af513",
          "createdAt": "2021-01-29T19:05:26.166Z",
          "updatedAt": "2021-03-27T09:46:07.300Z",
        },
        {
          "_id": "603fb4d4dd0c9134c8ce59b7",
          "item_id": "60145c769bd3b700087af513",
          "createdAt": "2021-01-29T19:05:26.166Z",
          "updatedAt": "2021-03-27T09:46:07.300Z",
        },
        {
          "_id": "603fb4d4dd0c9134c8ce59b7",
          "item_id": "60145c769bd3b700087af513",
          "createdAt": "2021-01-29T19:05:26.166Z",
          "updatedAt": "2021-03-27T09:46:07.300Z",
        },
  ]
}

就像我想将所有结果合并到 Root 结果中一样。我可以这样做吗?

【问题讨论】:

    标签: arrays mongodb mongoose mongodb-query aggregation-framework


    【解决方案1】:

    演示 - https://mongoplayground.net/p/xA14K2mLuiK

    db.collection.aggregate([
      { $unwind: "$inventory" }, // to individual documents
      { "$replaceRoot": { "newRoot": "$inventory" } } // replace the root with inventory value
    ])
    

    $replaceRoot

    用指定的文档替换输入文档。该操作将替换输入文档中的所有现有字段,包括 _id 字段。您可以将现有的嵌入文档提升到顶层,或创建一个新文档进行提升(参见示例)。

    $unwind

    从输入文档中解构一个数组字段,为每个元素输出一个文档。每个输出文档都是输入文档,其中数组字段的值被元素替换。

    【讨论】:

      猜你喜欢
      • 2018-07-17
      • 2021-09-29
      • 1970-01-01
      • 2017-07-26
      • 2023-02-24
      • 2021-02-26
      • 1970-01-01
      • 2021-07-07
      • 2014-10-29
      相关资源
      最近更新 更多