Mongodb Atlas 图表 - 在不同列中显示分组计数

标签 mongodb mongodb-atlas mongodb-charts

我的数据库中有一个“ofertas”集合,数据具有以下结构:

{
    "_id" : ObjectId("6057a995e5a26c119d254f35"),
    "empresa" : {
        "direccion" : {
            "direccion" : "concepcion arenal",
            "cp" : "36950",
            "provincia" : "pontevedra",
            "poblacion" : "Moaña",
            "pais" : "espana"
        },
        "nombre" : "caru sl"
    },
    "nombre_oferta" : "Offer Name",
    "referencia_interna" : "111222",
    "fecha_publicacion" : ISODate("2021-03-21T23:00:00.000Z"),
    "fecha_caducidad" : ISODate("2021-04-19T22:00:00.000Z"),
    "descripcion" : "Offer description",
    "estado" : "caducada",
    "pagada" : true,
    "userId" : "XXX",
    "candidatos" : [],
    "createdAt" : ISODate("2021-03-21T20:16:21.438Z"),
    "updatedAt" : ISODate("2021-04-20T22:08:14.221Z"),
    "__v" : 0
}

我有这个 MongoDB 查询:

[
    { $sortByCount: "$empresa.nombre" },
    {$group: {
         _id: 0,
         empresas_ofertas:{$push: {
             empresa:"$_id",
             ofertas:"$count",
         }},
    }},
    { $unwind: "$empresas_ofertas"}
]

这对“ofertas”的数量进行分组,如下所示:

query result

我想将 MongoDB Atlas 图表中的数据显示为单独的列,其中包含“empresas_ofertas”数组中每个对象的“ofertas”计数,但该图表仅显示一列,其中包含“ofertas”总量:

atlas chart

我将 Y 轴聚合从 SUM 更改为 COUNT BY VALUE,但它将数据与具有相同数量“ofertas”的项目分组。我想做的是显示“empresas_ofertas”数组中的所有对象以及 ofertas 的计数:第一列应为 60 (IXAS Value SL),第二列应为 53 (caru sl)...

我做错了什么?

最佳答案

第 1 步 - 创建查询

你的询问很好。它将返回您创建请求的图表所需的数据。我测试了它,您将得到以下格式的响应数据:

[
  {
    "_id": 0,
    "empresas_ofertas": {
      "empresa": "caru sl 2",
      "ofertas": 2
    }
  },
  {
    "_id": 0,
    "empresas_ofertas": {
      "empresa": "caru sl 1",
      "ofertas": 1
    }
  }
]

这是工作示例:https://mongoplayground.net/p/Gpn9trcgfsK

第 2 步 - 定义轴

正如您的图片所示,在左侧菜单列表中,您有 _id 字段和带有嵌套 empresaempresas_ofertas 对象字段和 ofertas 字段。这是从查询返回的数据结构。

在该列表的右侧,有一个部分,您应该根据可用数据(_idempresas_ofertas.empresaempresas_ofertas.ofertas)。请注意,现在您根本没有指定 X 轴,如图所示。

您所要做的就是将 empresa 嵌套字段拖放到 X 轴,并将 ofertas 拖放到 >Y 轴。这将显示您请求的图表。

关于Mongodb Atlas 图表 - 在不同列中显示分组计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67712338/

相关文章:

mongodb - 如何在 MongoDB 图表中绘制统一的时间序列

mongodb - 带有 “Package ' mongodb的Docker构建失败,没有候选安装程序”

java - MongoDB 与 Java : Finding the document with max value

javascript - Mongoskin 中的分页(跳过 + 限制)

mongodb - Mongorestore 一直向我抛出 "(NotMaster) not master"错误

mongodb - 官方 mongodb go-driver heroku 连接到 mongodb atlas 沙箱

mongodb - 按日、月、年获取不同的 ISO 日期

mongodb - 无法使用 Mongo Shell 连接到 MongoDB Atlas 集群

MongoDB 嵌入式图表过滤器