【问题标题】:How to add the results of a group, to the grouped documents? MongoDB如何将组的结果添加到分组的文档中? MongoDB
【发布时间】:2022-01-18 18:01:11
【问题描述】:

我通过传递这些数据来创建一些报告: Reports Model

userId : String,
marketId : String,
marketName : String,
itemId : String,
minPricePerKg : Number,
maxPricePerKg : Number

通过POST 请求创建 3 个报告:

POST /reports 

request 1:
{
  "details": {
    "userId": "1",
    "marketId": "1",
    "marketName": "market1",
    "itemId": "1",
    "minPricePerKg": "10",
    "maxPricePerKg": "20",
  }
}

request 2:
{
  "details": {
    "userId": "2",
    "marketId": "1",
    "marketName": "market1",
    "itemId": "1",
    "minPricePerKg": "20",
    "maxPricePerKg": "40",
  }
}

request 3:
{
  "details": {
    "userId": "1",
    "marketId": "2",
    "marketName": "market2",
    "itemId": "1",
    "minPricePerKg": "80",
    "maxPricePerKg": "100",
  }
}

我想获取某个特定itemId(从查询中收到)的所有报告的平均价格。 因此,为了便于理解,我们将过滤掉所有带有 itemId.. 和 $match : { itemId } 的报告

请求时 GET /reports?itemId=1

Expected Output

[
  {
    "marketId": "1",
    "itemId": "1",
    "marketName": "market1",
    "users": ["1", "2"],
    "minPrice": 15,
    "maxPrice": 30
  },
  {
    "marketId": "2",
    "itemId": "1",
    "marketName": "market2",
    "users": ["1"],
    "minPrice": 80,
    "maxPrice": 100
  }
]

这里minPrice 是所有minPricePerKg 的平均值,maxPrice 是所有maxPricePerKg 各自marketId's 报告的平均值。 我也想在结果中获得所有字段,即(marketId, marketName, users, itemId


我得到的输出是:

[
  {
    "_id": {
      "marketId": "market1"
    },
    "minPrice": 15,
    "maxPrice": 30
  },
  {
    "_id": {
      "marketId": "market2"
    },
    "minPrice": 80,
    "maxPrice": 100
  }
]

我的方法是这样的:

const res = await Report.aggregate([
  { $match: { itemId } },
  { $group: { _id: { marketId : "$marketId" }, minPrice: { $avg: '$minPricePerKg' }, maxPrice: { $avg: '$maxPricePerKg' } } },
  { $project: { "marketName": 1 } },
]);

【问题讨论】:

  • 您可以在 JSON 中添加示例数据并在 JSON 中添加预期输出吗?所以我们知道您拥有什么数据,以及您需要什么结果。
  • 编辑了问题..请再看一遍。

标签: node.js mongodb mongoose aggregation-framework


【解决方案1】:

查询

  • group 以保留组中常见的字段
  • 并积累那些不常见的usersMinPriceMaxPrice

Test code here

aggregate(
[{"$replaceRoot": {"newRoot": "$details"}},
  {"$group":  
    {"_id": 
      {"marketId": "$marketId",
        "itemId": "$itemId",
        "marketName": "$marketName"},
      "avgMinPrice": {"$avg": {"$toDouble": "$minPricePerKg"}},
      "avgMaxPrice": {"$avg": {"$toDouble": "$maxPricePerKg"}},
      "users": {"$push": "$userId"}}},
  {"$replaceRoot": {"newRoot": {"$mergeObjects": ["$_id", "$$ROOT"]}}},
  {"$project": {"_id": 0}}])

【讨论】:

  • 实际上没有。我想要的是首先将所有集合与一些itemId... ...现在从这些中,按照marketId对它们进行分组,然后取minPricePerKgmaxPricePerKg的平均值...并标记为minPricemaxPrice所以,基本上它变得独一无二marketId 响应的对象数量...以及哪些用户对此进行了报告 marketId 也被添加...
  • ok 更新有帮助,我认为这一次正是您所需要的。 (查询假设报表数据在数据库中)
  • 是的。它最初只需要删除{"$replaceRoot": {"newRoot": "$details"}},因为details在开始时不存在......谢谢。学习了$replaceRootnewRoot 之类的新东西...
猜你喜欢
  • 1970-01-01
  • 2014-07-29
  • 1970-01-01
  • 1970-01-01
  • 2020-02-23
  • 1970-01-01
  • 2012-09-28
  • 1970-01-01
  • 2016-10-16
相关资源
最近更新 更多