【问题标题】:MongoDB count without duplicate by given id给定 id 不重复的 MongoDB 计数
【发布时间】:2020-11-29 05:03:53
【问题描述】:

我是 Mongodb 的初学者,下面给出我的数据

{ "client_id" : 1, "invoice":[{"invoice_date" : "20-07-2020", "invoice_month" : "July 2020","account_status" : "Active"}]}
{ "client_id" : 1, "invoice":[{"invoice_date" : "20-07-2020", "invoice_month" : "July 2020","account_status" : "Active"}]}
{ "client_id" : 1, "invoice":[{"invoice_date" : "30-07-2020", "invoice_month" : "July 2020","account_status" : "Active"}]}
{ "client_id" : 2, "invoice":[{"invoice_date" : "03-08-2020", "invoice_month" : "August 2020","account_status" : "Active"}]}
{ "client_id" : 2, "invoice":[{"invoice_date" : "05-05-2020", "invoice_month" : "August 2020","account_status" : "Active"}]}
{ "client_id" : 3, "invoice":[{"invoice_date" : "10-06-2020", "invoice_month" : "June 2020","account_status" : "Active"}]}

我希望计数没有所有重复值,并在下面提到预期结果

{"client_id": 2 , "invoice_month" : "August 2020", "count": 1}
{"client_id": 1 , "invoice_month" : "July 2020", "count": 1}
{"client_id": 3 , "invoice_month" : "June 2020", "count": 1}

我在下面使用 aggregate,但它计数所有重复

[
    {
        "$match": {
            "client_id": "1",
            "invoice.account_status": "Active"
        }
    },
    {
        "$unwind": "$invoice"
    },
    {
        "$group": {
            "_id": '$invoice.invoice_month',
            "count": {"$sum": 1}
        }
    }
]

【问题讨论】:

  • 是什么导致文档重复?
  • 实际上我想根据月份和 client_id 获得计数,所以我不希望当月重复计数@TomSlabbaert
  • 所以我们不能计数,因为计数永远是 1?
  • 是的,我想根据月份为特定的 client_id 设置 1 @TomSlabbaert
  • 很抱歉,但我仍然不明白问题所在或您要达到的目标。看来您发布的聚合工作正常?

标签: mongodb mongodb-query pymongo


【解决方案1】:

下面提到的阶段将帮助您达到预期的结果

playground

db.collection.aggregate([
  {
    "$match": { //Considering only active accounts
      "invoice.account_status": "Active"
    }
  },
  {
    "$unwind": "$invoice" // reshaping the invoices
  },
  {
    $project: { //Converting string to date
      "invoice_date": {
        $dateFromString: {
          dateString: "$invoice.invoice_date",
          "format": "%d-%m-%Y"
        }
      },
      "client_id": 1
    }
  },
  {
    $sort: { //getting the latest invoice
      "invoice_date": -1
    }
  },
  {
    "$group": { //reshaping again to group the result
      "_id": "$client_id",
      "data": { //Only first record
        $first: "$$ROOT"
      }
    }
  },
  {
    $project: {//Projecting what is needed
      "_id": 0,
      "client_id": "$data.client_id",
      "invoice_date": "$data.invoice_date"
    }
  }
])

如果你想在输出中保留日期作为字符串,你可以改变最后一个阶段,如下所示

{
    $project: {
      "_id": 0,
      "client_id": "$data.client_id",
      "invoice_date": {
        "$dateToString": {
          date: "$data.invoice_date"
        }
      }
    }
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2022-01-26
    • 1970-01-01
    • 2019-12-02
    • 2022-11-01
    • 1970-01-01
    • 2013-10-07
    相关资源
    最近更新 更多