我有MongoDB数据库,有很多不同的硬币,需要在多级输出中聚合它们,但不确定如何进行.我正在使用Mongoose在后端,需要创建与express 路由,将获得硬币,并以我需要的方式排序,以便我在应用程序内的前端更容易.我在这里找到了很多多级群的例子,但没有一个与我的情况不符,我可以让它发挥作用.

-你是什么意思?

[
  {
    "_id": "643d5bf4637daa055466d8f0",
    "country": "AND",
    "denomination": 0.01,
    "series": 1
  },
  {
    "_id": "643d5bf4637daa055466d8f0",
    "country": "AND",
    "denomination": 0.01,
    "series": 1
  },
  {
    "_id": "643d5bf4637daa055466d8f0",
    "country": "BEL",
    "denomination": 0.01,
    "series": 1
  },
  {
    "_id": "643d5bf4637daa055466d8f0",
    "country": "BEL",
    "denomination": 0.02,
    "series": 1
  },
  {
    "_id": "643d5bf4637daa055466d8f0",
    "country": "BEL",
    "denomination": 0.5,
    "series": 2
  },
  {
    "_id": "643d5bf4637daa055466d8f0",
    "country": "ESP",
    "denomination": 0.2,
    "series": 1
  },
  {
    "_id": "643d5bf4637daa055466d8f0",
    "country": "ESP",
    "denomination": 0.05,
    "series": 3
  }
]

我需要这样的输出

-你在做什么?

[
  {
    "country": "AND",
    "series": [
      {
        "series": 1,
        "coins": [
          {
            "_id": "643d5bf4637daa055466d8f0",
            "country": "AND",
            "denomination": 0.01,
            "series": 1
          },
          {
            "_id": "643d5bf4637daa055466d8f0",
            "country": "AND",
            "denomination": 0.01,
            "series": 1
          }
        ]
      }
    ]
  },
  {
    "country": "BEL",
    "series": [
      {
        "series": 1,
        "coins": [
          {
            "_id": "643d5bf4637daa055466d8f0",
            "country": "BEL",
            "denomination": 0.01,
            "series": 1
          },
          {
            "_id": "643d5bf4637daa055466d8f0",
            "country": "BEL",
            "denomination": 0.02,
            "series": 1
          }
        ]
      },
      {
        "series": 2,
        "coins": [
          {
            "_id": "643d5bf4637daa055466d8f0",
            "country": "BEL",
            "denomination": 0.5,
            "series": 2
          }
        ]
      }
    ]
  },
  {
    "country": "ESP",
    "series": [
      {
        "series": 1,
        "coins": [
          {
            "_id": "643d5bf4637daa055466d8f0",
            "country": "ESP",
            "denomination": 0.2,
            "series": 1
          }
        ]
      },
      {
        "series": 3,
        "coins": [
          {
            "_id": "643d5bf4637daa055466d8f0",
            "country": "ESP",
            "denomination": 0.05,
            "series": 2
          }
        ]
      }
    ]
  }
]

到目前为止,我已经完成了这项工作,但不知道如何从这一点开始按正确的顺序推硬币.

const coins = await Coin.aggregate([
    {
      $group: {
        _id: '$country',
        series: { $addToSet: { series: '$series', coins: [] } },
      },
    },
    {
      $addFields: { country: '$_id' },
    },
    {
      $project: {
        _id: 0,
      },
    },
    {
      $sort: { country: 1 },
    },
 ]);

推荐答案

一种可能的方法是

  1. countryseries分组
  2. 仅按country重新分组,并将已分组的系列和硬币推送到系列数组
  3. 投影所需的字段并进行排序

您可以在Stage下拉列表中查看中间阶段 playground

db.collection.aggregate([
  { $group: { _id: { country: "$country", series: "$series" }, coins: { $push: "$$ROOT" } } },
  { $group: { _id: "$_id.country", series: { $push: { series: "$_id.series", coins: "$coins" } } } },
  { $addFields: { country: "$_id" } },
  { $project: { _id: 0 } },
  { $sort: { country: 1 } }
])

或者你可以直接$project,而不是$addFields

{ $project: { _id: 0, country: "$_id", series: 1 } }

Mongodb相关问答推荐

Select 筛选聚合中的嵌套字段

所有文档中嵌套 struct 中的条件匹配-mongodb

Spring数据MongoDB(聚合)

Tableau 与 Mongo DB Atlas by MongoDB 的连接缓存问题

MongoDB:如何从数组中的所有对象中删除属性?

我可以在 MongoDB 中将字段值设置为对象键吗?

如何使用指南针连接到 mongodb replicaset (k8s)

使用绝对类型在 Typescript 中编写 Mongoose 的类型化模型和模式的类和接口

使用 homebrew 和 Xcode 8.1.1 安装 Mongodb 失败

是否可以迭代 mongo 游标两次?

如何使用 Spring Data MongoDB 通过 GridFS ObjectId 获取二进制流

使用 brew upgrade Mongo update from 3.4 to 4.0 报错:在try 升级到 4.0 之前,数据文件需要完全升级到 3.6 版

如何使用 mongodb 和 php 正确处理分页查询?

如何使用 java 驱动程序更新 mongo db 中的文档字段?

MongoDB 日志(log)文件和 oplog 有何不同?

Mongodb KeyFile 太开放权限

$orderby 和 Sort 之间的 MongoDB 区别

Meteor订阅不更新集合的排序顺序

Mongoose 为所有嵌套对象添加 _id

查找聚合性能差